Sign in to follow this  
Winegums

Class Inheritence in C++

Recommended Posts

Winegums    286
Hi...so i have this object class... class Object { ... virtual void Update(); } and another called SmartObject that inherits from object clas SmartObject : public Object { ... void Update(): } theres also an object manager that each object is added to. this manager has a function that itterates through the list of objects and calls their update functions. From what i understood, the smartObjects update function should be called automatically since objects update function is virtual. is there something i'm missing?

Share this post


Link to post
Share on other sites
nmi    978
You store pointers to Object in the list of the object manager ? If not, that may be the problem. Can you post the relevant code from the object manager ?

Share this post


Link to post
Share on other sites
dave    2187
Sounds like you are on the right track. Can you paste all of the relevent code, there might be something you have excluded that is wrong.

Dave

Share this post


Link to post
Share on other sites
Illco    928
That depends on what the list holds. Let's assume we have some smart object instance:

SmartObject object;

Now we access it in various ways:

// This will call Object::Update();
Object plain = object;
plain.Update();


// This will call SmartObject::Update():
Object & reference = object;
reference.Update();


// This will call SmartObject::Update():
Object * pointer = &object;
pointer->Update();


So if the list holds Object instances it will not work; i.e. if the list is for example std::vector<Object>. You would have to use std::vector<Object*> for example.

Greetz,

Illco

(ok way too late but i hope it clears something up for someone sometime)

Share this post


Link to post
Share on other sites
Winegums    286
right...it works now and i only kinda think i know why...basically the base class update() took a parameter, so i made the inherited update take one too and it works. could this've been the problem?

Share this post


Link to post
Share on other sites
arithma    226
Quote:
Original post by Winegums
right...it works now and i only kinda think i know why...basically the base class update() took a parameter, so i made the inherited update take one too and it works. could this've been the problem?


This was indeed your problem.
You were overloading the function instead of overriding it.
For virtual functions to dispatch from a base interface the same signature of the function must be used (there are some rare exceptions that I can't really clearly/correctly elaborate on)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this