C++: protected - confused

Started by
17 comments, last by Qw3r7yU10p! 19 years, 7 months ago
Coming from java, i'm confused with protected in C++ class A{ protected: int i; }; class B: public A{ public: void set(const B& b){//1st case i = b.i;//Ok } void set(const A& a){//2nd case i = a.i;//Error } }; It seems that protected field is accessible for instances of actual class, but not for instances of superclass... What trick should i use to not expose i as public and have access inside superclass instances.(like in Java)
H.rnet
Advertisement
Clicky

I don't know java, but I think what you're looking for is protected members. Protected members are accessible to derived classes.
Your code snipplet kinda confused me, so I'll just stick with my gut and explain what protected means in c++.

Protected members (methods and variables) are accessible by derived classes, and classes of the same type. They are however not accessible by any other means.
Except friends classes/functions.
There's plenty of info at the C++ FAQ Lite.

On the whole you wouldn't ever have protected member data.
There are reasons for public data, and protected data, and private data. It's Not the Rules

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote:Original post by Washu
There are reasons for public data, and protected data, and private data. It's Not the Rules


Right, on the whole you wouldn't ever have protected data.
you can't get the b.i member out of the b object you pass in because it's protected, the same would be true of java.

I reckon you meant to write:

set( int value){
i = value;
}

or

set( B b ){
i = b.getI();
}
with a public getI method in A that just looks like:
int getI(){
return i;
}



both of which are fine. c++ is just like java in terms of protected, public etc. access

both your first and second cases look like they should fail to me
Try replacing <code> with <tt>.
Quote:Original post by Timberl
you can't get the b.i member out of the b object you pass in because it's protected, the same would be true of java.


Actually it's perfectly fine to access protected members of B objects from inside B's member functions.

I think the formating made it difficult to understand what the OP was asking. He wants to know why B can access A's protected members on B objects but it can't access them on A objects even though B derives from A.

class A {protected:    int i;};class B : public A {public:    void set(const B& b) { // 1st case        i = b.i; // Ok    }    void set(const A& a) { // 2nd case        i = a.i; // Error    }};


Derived classes can only access protected derived members on instances of the derived class, not on instances the base class. Protected means that derived classes can access protected members within themselve only.

Java's protected works differently to C++'s. The reason this works in Java is because members marked as protected are accessible by derived classes and classes in the same package. B would be able to access a.i even if B wasn't derived from B. If you put A and B in different packages you will get the same error as you do in C++.

IMO it was a bad design decision to make protected behave this way in Java. Apparently there was initially a fifth visibility option called private protected but it was removed in the name of simplicity.

This topic is closed to new replies.

Advertisement