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
4 answers

How to write a private attribute of a class?

I was working on a class which will contain an array of objects of another class. But I'm not able to do so. Here is a simplified version of the code I'm trying to use. Can anyone help me work with it? Why s cannot access its own data members? Can…
user11937123
0
votes
1 answer

Correct use of friend for using multiple strategies without losing state?

I have a class with some data, which I want to represent in different ways. There will always only be one representation active at a time, but I need to be able to change between them at runtime without losing internal state of any of the…
Julian
  • 41
  • 7
0
votes
1 answer

How do i get a function pointer to a friend function defined inside a template class

I want to define a template class Foo and a template function func, so that func is a friend of Foo, but func is NOT a friend of Foo for T1 != T2. As far as I know, there are two ways to do this. 1: template < typename T > class…
Sören
  • 1,803
  • 2
  • 16
  • 23
0
votes
1 answer

How to Edit/Access Private Parts of Windows Forms with external class? [in project]

I'm Working with Windows Forms (c++) and running into a bit of trouble :/ I have the Windows Form managed code where i do all the 'visual' work like updated text boxes, labels, etc etc... then i have class set up in that project which does most of…
AR2
  • 11
  • 2
  • 5
0
votes
1 answer

Compiler doesn't recognize friend function

A friend function can't be recognized #include #include class hello { private: int a, b; public: hello(int a, int b) { this->a = a; this->b = b; } friend int add(); }; int add() { return a +…
0
votes
0 answers

In what cases do I use friend while overloading operators of a class with private variables?

So, we are studying about the topics I mentioned in the title and it seems like I got it all pretty confused. Let's say for example that I have this class: class Complex { double re; double im; public: Complex(double r = 0, double i =…
איתן לוי
  • 433
  • 1
  • 3
  • 9
0
votes
2 answers

strange behavior with friend functions -- scope "globalized" when object pointer passed?

Let's say I have a header file a.h and a source file a.cpp. When I try to compile this and call what() from a different file (e.g. main.cpp) which includes a.h: a.h: class A { friend void what(); public: int index; }; a.cpp: #include…
0
votes
0 answers

How can I define a friend member function to a class that contains non-references/pointers of that class?

If I have two classes: one is Point and the second is Square, Point has member data: x and y those are "private" to represent the coordinates of the point. The class Square and similar ones implement the "has-a" relationship; so for example Square…
Syfu_H
  • 605
  • 4
  • 17
0
votes
1 answer

Declaring nested base template class instances the friends of a derived class

Suppose I have: class A {}; template class B {}; template class C {}; class D : public C> { //friend ... ?? }; Is there a way to construct a friend declaration for class D such that an instance of type A is a…
MattyZ
  • 1,541
  • 4
  • 24
  • 41
0
votes
1 answer

std::function and friend function

In this example, I have a pointer of function (std::function) as an attribute of my class. So I can associate any function of the form void myFunction(void) to my class. #include #include class Example{ private: int…
0
votes
1 answer

How to obtain difference of sets like behaviour with OOP

I need a behaviour similar to the set-theory's difference operator \. I am trying to get something of a friend class behaviour since i have the following situation: public interface IRead { List GetInnerResource(); } …
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
0
votes
1 answer

How to add class friends on different files headers?

My class Bloque is a friend of my other class user and i want to pass an int y of my user class to a function called void colision() on my Bloque class. So I tried this: # user.h # class user { private: int y; friend class…
precaca
  • 3
  • 2
0
votes
2 answers

Can't use private constructor of befriended CRTP class

I have code which uses this design, simplifed to get this MCVE - code and compiler errors follow. The basic problem is that I thought befriending the CRTP class would allow the templated base class access to the derived CRTP class's private members,…
davidbak
  • 5,775
  • 3
  • 34
  • 50
0
votes
2 answers

C++ access member-function from friend class

Is there a way to access member-function from the friend class? // foo.h template class A { bool operator()(Item* item) { ObjectClass c = get_class_from_item(item); // compiler error ... } }; class B { …
maverik
  • 5,508
  • 3
  • 35
  • 55
0
votes
2 answers

Friend function and method with the same name

The following classes definitions declare a friend function providing an inline definition for it. I was trying to invoke the friend function from a class method with the same name of the friend function, but in order to make it work I have to…
Costantino Grana
  • 3,132
  • 1
  • 15
  • 35