Skip to main content
GameDev.net gamedev.net
🔒 Locked

Making class private function a friend

Started by littlekid Nov 20, 2009 at 11:30 PM 6 replies 1.1k views
Original Post
littlekid
littlekid
Hi, how do I make a private member function a friend of another class?? e.g something like

class Foo
{
private:
    void Method();
};

class Bar
{
   friend void Foo::Method(); //this gives an error
};


Is there anyway to make Foo::Method() a friend of class Bar without making Method public?? Thanks.
dead1ock
dead1ock
class Foo{private:    void Method();};class Bar{   friend class Foo; //<-- this will give you access to all of Foos private members.};


There ya go.
Rattenhirn
Rattenhirn
If you want Bar to be able to call Foo::Method without making it public you can write the following:
class Foo{private:    friend class Bar;    void Method();};class Bar{};


Now, Bar is a friend of Foo and can touch its privates (that sounds a little dirty, actually...)

If you only want a certain method in Bar to be able to fondle with Foo, then you can write the following:

class Foo{private:    friend void Foo::MethodThatAccessesFoo();    void Method();};class Bar{public: //or whatever you like    void MethodThatAccessesFoo();};


So, the friend keyword...
* ...allows _another_ class and/or function to access _all_ private and protected members of a class
* ...is always used in the class that allows other classes or function access to its internals
* ...cannot be used to only allow access to certain members of a class. You'd need an interface or something similar for that.

Hope that helps!
littlekid
littlekid
Thanks for the replies, but I was seeking ways for the private Foo::Method to access Bar's private members. e.g something like

class Foo{private:    //I need to access the private members of bar in this method    void Method(Bar* barClass)    {        //for e.g only        barClass->Counter = 10;        barClass->Counter2 = 100;    }public:    void doWork(Bar* barClass)    {        Method(barClass);        std::cout << "Work Done!!\n";    }};class Bar{   friend void Foo::Method(); //this gives an errorprivate:   int Counter;   int Counter2;public:   void Sample();};


However the above gives an error saying that Bar cannot access Foo::Method() because it is private.
SiCrane
SiCrane
You can't make individual member functions a friend. You can only friend the whole class.
Codarki
Codarki
You can grant friend access to invidual members and functions via 3rd class. This way BarFriend has full access, but Foo has only access to set two data members.

class BarFriend{protected:    void SetCounter(Bar& bar, int value)    {        bar.Counter = value;    }    void SetCounter2(Bar& bar, int value)    {        bar.Counter2 = value;    }}class Foo : public BarFriend{public:    void Method(Bar& bar)    {        SetCounter(bar, 10);        SetCounter(bar, 100);    }};class Bar{private:    friend BarFriend;    int Counter;    int Counter2;};
Codarki
Codarki
But I doubt this is the answer for what you want to achieve. Tell us more why you want this, and I'm sure we can come up with more elegant solution.
littlekid
littlekid
Thanks for the quick replies. I think I will try and redesign the classes to make it more independent with each having a single responsiblity. I am actually implementing a factory pattern, and the friend was intented to restrict the creation of such objects to only the factory class methods. This is so that I do not accidentally create the classes myself.

Topic Locked

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

Sign in to reply to this topic.