1,105,286 Community Members

Restrict friend to one method

Member Avatar
Jamesbch
Newbie Poster
20 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello everyone,

I've found out that the friend keyword can be used with a class or a function/method but I'm wondering if it's possible to do something like this :

class A;
class B;

class A {
private:
  friend class B void AddEl(void *el);
  friend class B void RemEl(void *el);
  B *myB;
}

class B {
  inline void Do() {
    myA->RemEl(el2);
    myA->AddEl(el1);
  }
  private:
   A *myA;
}

In my code there is only one method of B (Do here) which needs the two private methods of A (AddEl and RemEl here). I can't use friend <method> because my class A is only forward-declared at this point (could be easily solved in the example above but not in my code). I can use friend class <class> but I think it's against the OOP and not safe.
Is there any way to declare friend of a class to methods like above ? Many thanks in advance.

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
0
 

In order to do that you need to use the scope resolution operator which is '::'
Observe:

#include <iostream>
using namespace std;

class Alpha;                           //forward declaration of class Alpha

class Bravo {                          //begin declaration of class Bravo
private:
  Alpha* m_pAnAlphaObj;                //a pointer to Alpha

public:
  Bravo();                             //default constructor
  ~Bravo();                            //destructor
  void memberMethod1();                //member method
  void memberMethod2();                //member method
};

class Alpha {                          //begin declaration of class Alpha
  friend void Bravo::memberMethod1();  //declare just Bravo::memberMethod1() as friend

private:
  int prvInt;

public:
  Alpha() { this->prvInt = 5; }        //Alpha default constructor
  Alpha(int newPrvInt) { this->prvInt = newPrvInt; }            //Alpha specified constructor
  Alpha(Alpha* pNewAlpha) { this->prvInt = pNewAlpha->prvInt; } //Alpha copy constructor
  int getPrvInt() { return prvInt; }   //member method
};

Bravo::Bravo() { m_pAnAlphaObj = new Alpha; }   //implement Bravo constructor

Bravo::~Bravo() { delete m_pAnAlphaObj; }       //implement Bravo destructor

void Bravo::memberMethod1() {          //implement member method
  cout << "In Bravo::memberMethod1()\nThe value of m_pAnAlphaObj::prvInt is: " << m_pAnAlphaObj->prvInt << endl;
}

void Bravo::memberMethod2() {          //implement member method
  //compile error, illegal access
  //cout << "In Bravo::memberMethod2()\nThe value of m_pAnAlphaObj::prvInt is: " << m_pAnAlphaObj->prvInt << endl;

  //legal, uses public method
  cout << "In Bravo::memberMethod2()\nThe value of m_pAnAlphaObj::prvInt is: " << m_pAnAlphaObj->getPrvInt() << endl;
}

int main() {                          //begin main()
  Bravo randomBravoObject;            //declare a Bravo object

  randomBravoObject.memberMethod1();  //call member method
  randomBravoObject.memberMethod2();  //call member method

  cin.get();
  return 0;
}

I wouldn't say friend classes are "against OOP", but you really need to use them sparingly. Too many friends are bad, friends are not bad in general. There are some things that are best accomplished via friends. Always consider another approach first though, such as aggregation/composition or inheritance.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article