PDA

View Full Version : virtual overloaded functions and base class function call...



nouknouk
10th March 2006, 15:15
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 :


class gs_object {
...
virtual void createList();
...
};



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

jpn
10th March 2006, 15:26
Yes, this is a pure c++ "problem". :)



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.

nouknouk
10th March 2006, 15:39
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 ?

McToo
10th March 2006, 16:13
(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/library(d=robot)/c36yw7x9.aspx

HTH

McToo

jacek
10th March 2006, 16:29
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.


#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;
}

$ ./a.out
a.foo() : A::foo()
b.foo() : B::foo()
static_cast : B::foo()
dynamic_cast : B::foo()
b.bar() : A::foo()

nouknouk
10th March 2006, 18:23
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.

sunil.thaha
11th March 2006, 06:44
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/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/overload_member_fn_base_derived.htm

McToo
11th March 2006, 22:26
Nice one jacek & sunil!

Thanks for the code post, ended a misconception that may have cost me some time in the future, although to call a base class function I wouldn't have used the static_cast call anyway. (Honest, guv!) :cool:

McToo