virtual overloaded functions and base class function call...
Hello,
I've got a comprehension problem of what happend in one of the project
i'm working on. It's (I think) a pure c++ problem...
Basically I've got a class gs_object than has got a VIRTUAL function
createList(). This createList() function is overloaded in another class
named ct_server that inherits gs_object.
in my code, it looks something like that :
Code:
class gs_object {
...
virtual void createList();
...
};
Code:
class ct_server : public gs_object {
...
virtual void createList();
void initInstance();
...
};
Here is the problem : in the function ct_server::initInstance, one boy of my team wanted to
call the gs_object::createList() base function, and not the overloaded
one (ct_server::createList() ). But, according to me he made a mistake
as he wrote :
(static_cast<GS_object*>(this))->createList();
instead of
gs_object::createList();
According to me, as createList() is virtual, this line of code should
call ct_server::createList and not gs_object::createList()
But it doesn't : when in run in debug mode, i can see it calls
gs_object::createList();
I can't understand why. Could you explain me ?
FYI, i'm using Visual C++ 7.1.3 ; Qt 3.3.4
Re: virtual overloaded functions and base class function call...
Yes, this is a pure c++ "problem". :)
Quote:
Originally Posted by nouknouk
According to me, as createList() is virtual, this line of code should
call ct_server::createList and not gs_object::createList()
But it doesn't : when in run in debug mode, i can see it calls
gs_object::createList();
So which one you want it to call?
With a scope operator "::" it was directed it to call base class implementation, not the overloaded one.
Re: virtual overloaded functions and base class function call...
I wanted to call gs_object::createList, but that's not the problem in fact. With the syntaxt gs_object::createList(); it will work fine...
My problem is that I don't understand why what I consider to be a mistake works althrough It shouldn't.
(static_cast<GS_object*>(this))->createList();
Calling this for me is equivalent to do something like :
GS_object * myObj = (gs_object*)myCtServer;
myObj->createList();
but, as createList() is virtual, I should call ct_server::createList() even if the call is made from a gs_object pointer.
So why does it works anyway ?
Re: virtual overloaded functions and base class function call...
(static_cast<GS_object*>(this))->createList();
This line will call the base class implementation because of the static_cast<GS_object*> call. static_cast will convert a pointer to a base class pointer.
More info here...
http://msdn2.microsoft.com/en-us/lib.../c36yw7x9.aspx
HTH
McToo
Re: virtual overloaded functions and base class function call...
Quote:
Originally Posted by McToo
This line will call the base class implementation because of the static_cast<GS_object*> call. static_cast will convert a pointer to a base class pointer.
But that method is virtual, so cast shouldn't change anything.
Code:
#include <iostream>
class A
{
public:
virtual void foo() { std::cerr << "A::foo()" << std::endl; }
};
class B : public A
{
public:
virtual void foo() { std::cerr << "B::foo()" << std::endl; }
void bar() { A::foo(); }
};
int main()
{
A a;
B b;
std::cerr << "a.foo() :\t";
a.foo();
std::cerr << "b.foo() :\t";
b.foo();
std::cerr << "static_cast :\t";
( static_cast< A* >( &b ) )->foo();
std::cerr << "dynamic_cast :\t";
( dynamic_cast< A* >( &b ) )->foo();
std::cerr << "b.bar() :\t";
b.bar();
return 0;
}
Code:
$ ./a.out
a.foo() : A::foo()
b.foo() : B::foo()
static_cast : B::foo()
dynamic_cast : B::foo()
b.bar() : A::foo()
Re: virtual overloaded functions and base class function call...
ok. I've got the solution :
in fact, the declaration of my overloaded function in the derived class wasn't exactly the same as the base one (a parameter difference).
That's why It was still calling the base function.
Re: virtual overloaded functions and base class function call...
There is a difference between Overloading and Overridding
While you overload function in Derived class
Take Care ...
Have a look at this http://publib.boulder.ibm.com/infoce...se_derived.htm