Operating System - Linux
1752794 Members
6072 Online
108789 Solutions
New Discussion

Strage behaviour un calling polymorphic function

 
Dibendu_1
New Member

Strage behaviour un calling polymorphic function




I am seeing very strage behaviour in my application in terms of calling polymorphic function through derived class object.

My base class has around 13 virtual/pure virtual functions.While calling one of pure virtual function (which is overridden by delived class) through derived class object pointer, I am seeing it is calling another pure virtual function ( the over ridden version of derived class).

To demonstrate it more clearly here is the example:

class base{
public:
virtual int func1()=0;
virtual int func2()=0;
};

clasee derived: public base

{
public:

int func1(){ return 7;}
int func2() { return 30;}

}

Now while calling func2() function: ptr->func2() ( where "ptr" contains derived object's address), it is calling ptr->func1(). This I came to kwno when I stepped in through code using gdb.



Note that in our application the base class code is a differetn compilation unit ( say unit1), derived class part in different compliation unit,unit2 ( of cource while compiling it refers the existing base class library ). At last the object creation of delrived class ( usign factory design ) and calling functions is in a third compilation unit(unit3).

This specific problem I am seeing if unit3 is not built with unit1 and unit2.As there is no code chnage in unit3 compilation unit, while execution it is referring updated unit1 and unit2. Under this circumstance I am facing the above mentioned problem( I am calling ptr->func2() but practically it is calling ptr->func1() )

Whether any specific rule of polymorphism missing here or any linking factor..
1 REPLY 1
Dennis Handly
Acclaimed Contributor

Re: Strage behaviour un calling polymorphic function

Is base in a common .h file that all units are including? And no #if to change the class members?
If you don't do this, you are violating the ODR rule where all definitions must match.