views:

735

answers:

7

I haven't done C++ in a while and can't figure out why following doesn't work:

class A {
protected:
  int num;
};

class B : public A {
};

main () {
  B * bclass = new B ();
  bclass->num = 1;
}

Compiling this produces:

error C2248: 'A::num' : cannot access protected member declared in class 'A'

Shouldn't protected members be accessible by derived classes?

What am I missing?

+5  A: 

Yes, protected members are accessible by the derived class, but only from within the class.

example:

#include <iostream>

class A {
   protected:
   int num;
};

class B : public A {    public:
   void printNum(){
      std::cout << num << std::endl;
   }

};

main () {
   B * bclass = new B ();
   bclass->printNum();
}

will print out the value of num, but num is accessed from within class B. num would have to be declared public to be able to access it as bclass->num.

jhufford
+15  A: 

yes protected members are accessible by derived classes but you are accessing it in the main() function, which is outside the hierarchy. If you declare a method in the class B and access num it will be fine.

oykuo
Thank you! I see my mistake now
Roman M
+5  A: 

It is accessible within the scope of B's functions, but you are attempting to access it in main.

Jim Barnett
+4  A: 

But you're not accessing it from the derived class. You're accessing it from main().

jeffamaphone
+1  A: 

"Protected" means protected from access outside of a member function, or a member function of a derived class. The "main" function isn't a member of either class, but it's trying to directly access the member variable.

Mark Ransom
+1  A: 

You are seeing exactly what's expected. Try this - http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/ for info on inheritance access specifiers.

spitfire
+1  A: 

When utilizing a class, there really is no difference between protected and private members. Neither are accessible to anything that utilizes the class.

class A {
    private: int privateNum;

    protected: int protectedNum;

    public:  int publicNum;

    void SetNumbers(int num) {
        privateNum = num; //valid, private member can be accessed in member function
        protectedNum = num; //valid, protected member can be accessed in member function
}

void main() {
    A classA;
    classA.privateNum = 1; //compile error can't access private member
    classA.protectedNum = 1; //compile error can't access protected member
    classA.publicNum = 1; //this is OK
    classA.SetNumbers(1);  //this sets the members not accessible directly
 }

The difference comes into effect when you inherit from a class with protected members.

class B : public A {
}

All private members of a base class are still private, and will not be accessible to the derived class. The protected members, on the other hand, are accessible to the inherited class, but are still not accessible outside of the inherited class.

class B : public A {
 public:
   void SetBNumbers(int num) {
       privateNum = num; //compile error, privateNum can only be accessed by members of A, not B
       protectedNum = num; //this works, as protected members can be accessed by A and B
   }
}

void main() {
  B classB;
  classB.publicNum = 1; //valid, inherited public is still public
  classB.protectedNum = 1; //compile error, can't access protected member
  classB.privateNum = 1; //compile error, B doesn't know that privateNum exists
  classB.SetBNumbers(1); //this sets the members not accessible directly
}
bsruth