Skip to main content
gamedev.net
🔒 Locked

Inheritance

Started by jonbell 2 replies 250+ views
Original Post
jonbell
jonbell
I have 3 classes A, B, C B inherits from A and C inherits from B I create an instance of class C and in the constructor I call the constructor of class B manually B::B so that the data in class B will get initialised properly. However the data in class B does not get initialised at all. My code looks like this : C::C( ) { // call the base constructor B::B(); } I have debugged and the call to B''s constructor takes place but the data from B inside class C is still garbage. What am i doing wrong?
twanvl
twanvl
You don''t need to call the constructor manually, it will be called automatically BEFORE C''s constructor. The data becomming garbage must have another cause.
Greatwolf
Greatwolf
Do what fallenangel suggested and use the initializer list for class C. Once you''re inside the braces, your object already has it''s memory allocated. Normally the default base constructor is call automatically so you usually use this method if you need to also pass some arguments to the constructor.

Hope that helps





--{You fight like a dairy farmer!}
--{You fight like a dairy farmer!}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.