Sign in to follow this  
Farraj

C++ calls the wrong virtual function

Recommended Posts

First, I would like to say sorry for not using a spell check, it's not working for some reason. Also, sorry for not giving a lot of detials, it's pretty stright forward. I have a base class with a vritual function: class BaseClass { public: virtual void update(void) = 0; }; now, I have 3 classes derived from BaseClass, and all with their own update() functions: class Class1 : public BaseClass { public: void update(void); } and the same for Class2 and Class3 Now when I do this: BaseClass *testClass = new Class3(); and I call update: testClass->update(); C++ should call the update() function that belonges to Class3. I know this and done it many times in many projects. The crazy thing is, C++ keeps calling the update() function that belonges to Class1 :/ The code is simple and straight forward. Does anyone know why something like this would happen?? Thanks.

Share this post


Link to post
Share on other sites
I've never had this problem, but it could be an intellisense mishap. Try deleting the NCB and PDB files and rebuilding the solution. Also, make sure you are debugging in the Debug build.

Share this post


Link to post
Share on other sites
Quote:

class Class1 : public BaseClass
{
public:
void update(void);
}


This is missing a semicolon on the end and thus won't compile. When diagnosing C++ code, please always paste your actual code. Really. I want to see the real source code for Class1 and Class3. It sounds like Class3 is supposed to inherit from Class1 or 2? If Class2, please post that as well.

Copy and paste. Don't introduce transcription errors.

I'm guessing your update function signatures aren't as uniform as you think they are. Please also copy and paste your test invocation code.

Share this post


Link to post
Share on other sites
If your classes are in separate DLL's or something then it's because the you need to rebuild one or all of the projects so the interface is known across all. I've seen the problem quite often when the engineer forgets to rebuild either the client or the host library after adding, modifying, or removing an interface member function.

Of course, I have no idea how your project is structured, but that's when I've seen it.

Share this post


Link to post
Share on other sites
Just a shot in the dark:
If the code in the functions in question produced identical assembly when compiled and you are running a release build, then I wouldn't be surprised if it optimised out the extra functions that produced identical assembly and so in a release build it appeared to call the wrong one sometimes. I have seen this happen when debugging release builds! It is not a bug though, the program behaves as expected.
You still need to post the real code if it is a real problem.

Share this post


Link to post
Share on other sites
Assuming Class3 inherits Class2, and Class2 inherits Class1; then "void update()" needs to be declared virtual in both the Class1 and Class2 declarations. Currently, update() is not virtual in Class1, so all pointers to Class1 and its parent classes have no way to dynamically link child implementations of update().

Share this post


Link to post
Share on other sites
WhatsUnderThere, you are wrong. there is no way to make function not virtual if it was declared virtual somewhere before in base class

class A1
{
public:
virtual void foo() {cout<<"A1"<<endl;}
};

class A2 : public A1
{
public:
void foo() {cout<<"A2"<<endl;}
};

class A3 : public A2
{
public:
void foo() {cout<<"A3"<<endl;}
};

class A4 : public A3
{
public:
void foo() {cout<<"A4"<<endl;}
};




void main()
{
A1* p = new A4;
p->foo();
delete p;
}

the result output will be: A4

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