private virtual?

Started by
6 comments, last by Sneftel 18 years, 8 months ago
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?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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).
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
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.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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.

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.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
This article might explain why you would want to do something like create a private virtual method.
Wait, so was I wrong, right, or did my post not make sense?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
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.

This topic is closed to new replies.

Advertisement