Questions tagged [friend]

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

Different programming languages use the friend keyword differently. Make sure it is clear which language you are asking about.

1518 questions
0
votes
0 answers

How to declare a friend operator with template then define it

I have a class with two friend operator overloadings inside: template class A { T i; template friend auto operator+(const A &l, const A &r) -> A; template
Nathanael Demacon
  • 1,169
  • 7
  • 19
0
votes
1 answer

Template function inside template class "is private within this context" error

I'm trying to write my own class which is supposed to represent a templated array. I wanted to define operator+ for it but in a way that it can add two arrays specified with different types(ofcourse if one can be promoted to the other and vice…
0
votes
1 answer

How to make a templated class a friend of another templated class

For educational purposes I'm writing a templated Stack class based on a singly linked list. I have written a class for a node: template class StackNode { private: T data; StackNode *next; // TODO: make…
alekscooper
  • 795
  • 1
  • 7
  • 19
0
votes
1 answer

Remove friend class dependency in the following case

class foo { bar b; someFunction() { b.alphaObj->someFunctionOfAlpha(); } }; class bar { friend class foo; // many more friends private: alpha *alphaObj; }; How do I remove the friend dependency without…
0
votes
1 answer

templated operator overloading cannot bind non-const lvalue reference

I have tried variations of const and passing by reference, but seem to have problems down every corridor. This configuration gives the error cannot bind non-const lvalue reference of type ‘thingArr&’ to an rvalue of type ‘thingArr’ what am I doing…
0
votes
1 answer

Pattern for removing default constructor which leaves data in "bad state"?

Let's say you have a class which have members which should always be set (i.e. defaults would leave it in a bad state). To prevent having objects with "bad state", you have a constructor which requires everything to be set. The issue is, that you…
bpeikes
  • 3,495
  • 9
  • 42
  • 80
0
votes
1 answer

esms.cpp:234: error: 'the_config' was not declared in this scope

I'm trying to compile some .cpp files from an old open source project that no longer has a support community. There are about 15 .cpp files in the project and several of them use a one common file called config.cpp. I compile the c++ source code…
Dori
  • 3
  • 3
0
votes
1 answer

will this OO win32API example work with multiple windows?

i'm mainly focused on how this example uses wndproc as friend... im a little confused how it works and im just trying to figure out if and how this would work with more than one window http://www.uta.fi/~jl/pguibook/api2oo.html
gizmo
  • 1
  • 2
0
votes
0 answers

Issue with an istream friend function in a class receiving input

I have a class for working with rational numbers with three files and my goal is to be able to satisfy this output correct output: Enter operator (+,-,*,/,==,>=,<=,!=,<,>,-1 for negation): - Enter the two operands (ex. 1/2 or -3/4): 2/3…
0
votes
0 answers

Using friend operator overload function in template class, error undefined reference

Here is my code and declarations, im not sure how to properly set up friend functions with template classes. Can someone please guide me on what I did wrong and why? Thanks! private: int size; T* buff; friend Vector operator *…
Drunkhydra
  • 47
  • 2
0
votes
6 answers

encapsulation and friendship in C#

I have a particular case in my current project. I have: public class A { // etc. } public class B { // etc. private void HandleSomeEvent(object parameter) { // Etc. } protected void HandleSomeOtherEvent(object parameter) …
paercebal
  • 81,378
  • 38
  • 130
  • 159
0
votes
0 answers

Cannot access friend classes private variable in overloading "<<" and ">>" (Using multiple files)

I was just learning about friend classes and overloading operators in C++ where I came upon an issue where I cannot access the private variables in the friend class. I'm almost sure this has to do with the fact we were required to have each file…
drayk73019
  • 17
  • 5
0
votes
1 answer

How to make a specialized function template a friend to some class?

I'm trying to find a way to make a function that is a friend to a given class. That function is another class' method and is a specialization of a template. Without specialization, I have the following compiling code in Visual…
MajinSaha
  • 188
  • 1
  • 9
0
votes
1 answer

Additional template arguments for friend declaration in template class?

In order for some global function template void func (const Obj& obj) { for (int i = 0; i < count; i++) std::cout << obj.value << std::endl; } to be able to access to the private field value of some templated…
Cubi73
  • 1,891
  • 3
  • 31
  • 52
0
votes
3 answers

overload == to compare two linked list recursively

I have a homework problem, which asks me to overload == operator to compare two linked-list. And i need to do this in recursion. Here is my .h file class LList { public: friend bool operator == (const LList& lfSide, const LList& rtSide); …
Keric Ma
  • 17
  • 5