Advertisement Jump to content
Sign in to follow this  
luasitdown

Access right

This topic is 4904 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

// .............1
class A
{
private:
	int i;
public:
	get(A & a){a.i = 5;}
};

class B : public A
{
public:

};
//.................2
class A
{
protected:
	int i;
public:
	
};

class B : public A
{
public:
	get(A & a){a.i = 5;}
};

1 work ,2 not. why?

Share this post


Link to post
Share on other sites
Advertisement
Private data isn't available to derived classes, but protected data is. That's how private and protected are defined.

Share this post


Link to post
Share on other sites
In example 2, the one that doesn't work, the data is protected rather than private...

Why doesn't it work? Privateness and protectedness are there in the language to help keep you from shooting yourself in the foot, and they do the best they can without taking too much control away from you. If you were trying to do something like this in your program, I'd definitely call it shooting yourself in the foot (i.e. bad practice).

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!