Jump to content
  • Advertisement
Sign in to follow this  
hincubator

C++: protected - confused

This topic is 5409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!