Recommended Posts

I just ran across something that doesn't seem to make sense to me but that works and I would like to know how it's possible and what could be the possible consequences from it.
I have been testing the inheritance features of C# such as constructors overloading over multiple derived classes and then I found something weird due to a mistake I made.
Let's say I have the namespace Game.Engine.Init
I have made an abstract class called InitGame, and a derived class InitControls inheriting InitGame. In both classes I put some bogus variables to test and I found out that when I call this:
Game.Engine.Init.InitGame j00 = new Game.Engine.Init.InitControls();
this works for some unknown reasons and also I cannot use the variables in the InitControls Class. So I assumed that I specified of the class as InitGame using the InitControls constructor which first calls the InitGame CTor but I don't know how it can work after that because it's suposed to continue on with the InitControls CTor which would make no sense since the variables from the class are not avalaible in the base class.
Any idea guys ? This is weird and I need to know what happened there.
[Edited by - Avenyr on August 27, 2005 5:30:41 PM]

Share this post

Link to post

Share on other sites

InitControls is a InitGame, that is what inheritance does. The reason you can't access the InitControls variables is because you are using it as an InitGame, and can only access what is defined as part of InitGame. Other than this, I'm, not sure what you are asking about, so try clarifying.

0

Share this post

Link to post

Share on other sites

What I want to know is why does the InitControl Constructor works when the base class is specified because the line in the InitControl CTor where it sets the ControlBogus variable should fail since the base class does not contain this variable.

I understand that since the object is of the base class and not the derived class that I cannot access the derived class' variable.

0

Share this post

Link to post

Share on other sites

When you have a type declaration on the left hand side of the assignment has no impact on what the right hand side does. The right hand side in this case creates an object of the derived class and then assigns a reference to that derived class to a base class reference. The derived class does have that variable so all is good.