-4

I'm getting this error in the inheritance (C++) below :

B.cpp:11:9: error: 'isMember' is a protected member of 'A'
   if(x->isMember())

I've seen that a declared protected member in a "mother" class is reachable from a a member of the "daughter" class. But i still don't figure out what's the problem here. Here is the definitions of the my two classes A and B :

#ifndef _A_H_
#define _A_H_
class A 
{

  private:
  bool _member;

  public:
  A();
  virtual ~A();

  protected:
  bool isMember();
   };
#endif // _A_H_

//A.cpp
#include "A.h"

A::A(){_member=true;}

A::~A(){};

bool A::isMember()
{
    return _member;
}

//B.h

#ifndef _B_H_
#define _B_H_
#include "A.h"

class B : public A 
{
 private:
    A * _memberB;

public:
  B( A *x);
  ~B();

};
#endif // _B_H_

//B.cpp

#include "B.h"
#include "A.h"

B::B(A * x)
{
    if(x->isMember()) // call of the protected member of class A
    this->_memberB=x;  
}

B::~B()
{
    //cout<<"B--"<<endl;
    delete this->_memberB;  
}

//main.cpp
#include "B.h"

int main()
{
  A * a=  new A();

  B * b= new B(a);

    return 0;
}
Anass
  • 251
  • 2
  • 15
  • Because you're trying to access the method from an `A` object. Iirc, protected means a sub-class can use the member within it's self, but it can't access the parent's member; only its own. – Carcigenicate Oct 30 '16 at 10:54
  • yes, but why an object of the parent class cannot call its method in the sub class. This what i'm not understanding – Anass Oct 30 '16 at 11:15
  • Inside the B class, you can call isMember on B objects. You can't call isMember on A objects from within B. That's just what protected does. – Carcigenicate Oct 30 '16 at 11:17
  • Ok so i understand that it's the rule. – Anass Oct 30 '16 at 11:29
  • However, if i let the method "isMember" public, then the code will work, however this method will be reachable outside the classes and this is what i'm afraid of. Is there any solution to solve this ? – Anass Oct 30 '16 at 11:31
  • Possible duplicate of [Why can't my object access protected members of another object defined in common base class?](http://stackoverflow.com/questions/17717027/why-cant-my-object-access-protected-members-of-another-object-defined-in-common) – StoryTeller - Unslander Monica Oct 30 '16 at 12:00

1 Answers1

0

protected in C++ is per-class not per-object basis

per-object:
you cant access parent class's protected member through child object from outside even though child can actually access parent's protected function:

Child c;
c.protectedFunction(); // wont work, because you are calling protectedFunction() outside the child object.

per-class
but you can access it through child class:

class Child : public Parent
{
    public: 
        void someFunction()
        {
            protectedParentFunction(); // it works! you are callign it from Child class
        }
}

to fix your problem, you should write a public function inside the Child class that calls the protected function from Parent class.

class Child : public Parent
{
    public: 
        void callerOfProtectedFunction()
        {
            protectedParentFunction();
        }
}
Child c;
c.callerOfProtectedFunction();
Lorence Hernandez
  • 1,189
  • 12
  • 23
  • i understand what you are meaning. But it doesn't solve my problem. My problem is that iif i make the method "isMember" public, then the code will work, however this method will be reachable outside the classes and this is what i'm afraid of. Is there any solution to solve this ? – Anass Oct 30 '16 at 11:40
  • @Anass thats why i said to make a public function that calls protected function – Lorence Hernandez Oct 30 '16 at 11:45
  • @Anass theres only about 2 ways, create public function in class B that calls protected function of class A, or make your class B a friend of class A. – Lorence Hernandez Oct 30 '16 at 11:47
  • in your answer, the call " protectedParentFunction()" in the public function you made, does not integrate the object of the parent class. If you look to my code, i must call the protected function from the parent-class object. you see what i mean ? – Anass Oct 30 '16 at 12:07
  • @Anass the solution goes for parent class too :) create a public function there that calls its protected function. – Lorence Hernandez Oct 30 '16 at 12:10
  • Yes, it works now. Thank you for you patience and explanations :) – Anass Oct 30 '16 at 12:20