Skip to main content
gamedev.net
🔒 Locked

Understanding Friend

Started by The C modest god Feb 22, 2005 at 2:32 AM 5 replies 550+ views
Original Post
The C modest god
The C modest god
I would like to have in class A a private method which can be used by a public method of class B. The private method of class A have a this parameter (thats how method is defined?). Can this be achieved with friends?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
jeroenb
jeroenb
If you want to access a private member variable from another class you must indeed use the friend keyword. As an simple example:

class A {private:   int a;};class B {public:   int getA () {      return m_a.a;   }protected:   A m_a;};


In this case the compiler will complain as you are trying to access a private member of class A. You then have two options. You can add 'friend B' to class A to let the compiler know that class B may access its private member variables. A better OO way is to create a set/get pair of public member functions in class A that take care of the variable:

class A {public:   // either:   void setA (int _a) { a = _a; }   int getA () { return a; }   // or   friend B;private:   int a;};class B {public:   int getA() { return m_a.getA (); }protected:   A m_a;};

Now the compiler will not complain anymore.
Crafter 2D: the open source 2D game framework ?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]
Drew_Benton
Drew_Benton
Quote:
Original post by jeroenb
ps. how can I make code look like code on this forum?


Use the tags at the bottom on this FAQ. I can't fiure out how to paste tags correctly, so that's why I posted the link there [wink].

- Drew
ToohrVyk
ToohrVyk
You can create code using [ code ] [ /code ] or [ source ] [ /source ] blocks (without spaces).


As for preferring public getters/setters to a friend class, it all depends on the coupling between the two classes. Public getters/setters will always go against information hiding more than friendship because it asserts the existence of a state that they allow access to that should in fact only be known to the friend class.

The biggest example would be the coupling between a list and a list iterator, where the getter/setter used by the iterator to access list elements should not be made public ever (because it makes available to everyone the access method used by the iterator, hence making it hard to change.

If your two classes serve a very close purpose and are almost always meant to be used together, use friend using friend class B; inside class A. If a class starts having a lot of friends only for a given method, you might want to change your approach and make the method public.
The C modest god
The C modest god
Is there no way to make only one of the private parameters(methods) in class A available to use by class B?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Oluseyi
Oluseyi
Quote:
Original post by The C modest god
Is there no way to make only one of the private parameters(methods) in class A available to use by class B?
No. B will have access to all of A's members.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.