Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

private virtual?

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

Consider the following code:
		class C {
			public:
				DWORD counter;
				void foo2() {this->foo();};
			private:
				virtual void foo() {counter=1;};
		};
		class B:public C {
		        public:
				void foo(){counter=2;};
			private:
		};
		B b;
		b.counter = 0;
		b.foo2();
I have run this code and got counter equal to 2. I want to be sure what is happening here. I can overload a method in the parent class that was private? More then that, the method in the son class became public? Maybe I am incorrect and something else happens?

Share this post


Link to post
Share on other sites
Advertisement
You made the virtual function public by placing the definition/decleration there. It also doesn't matter if an overloaded function is private. Remember, inside the class nothing is really hidden. Even if you inherited privately, it wouldn't matter (except counter would be private, therefor the compiler would get mad).

Share this post


Link to post
Share on other sites
I thought that the son class inherits only the public and protected of its parent? So class B cannot use private methods or private members of class C.
However, it can overload a private virtual method in C? That is odd.

Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
I thought that the son class inherits only the public and protected of its parent?

No, it inherits everything; it just doesn't have access to private members.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by The C modest god
I thought that the son class inherits only the public and protected of its parent?

No, it inherits everything; it just doesn't have access to private members.

Ok, thanks.

Share this post


Link to post
Share on other sites
Your post kind of didn't make sense. C::Foo remains private. You couldn't call it from B if you wanted to. But you *can* override it.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!