Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual overloaded functions and base class function call...

Reply
Thread Tools

virtual overloaded functions and base class function call...

 
 
alexandre.braganti@gmail.com
Guest
Posts: n/a
 
      03-10-2006
Hello,
First sorry for my poor English, I am French
I've got a comprehension problem of what happend in one of the project
i'm working on.

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

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      03-10-2006
On 10 Mar 2006 06:13:00 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>Hello,
>First sorry for my poor English, I am French
>I've got a comprehension problem of what happend in one of the project
>i'm working on.
>
>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


Where is ct_server::initInstance() called? If it is called in the
constructor of the ct_server object, it is entirely possible that the
base class version is called because the object has not yet finished
construction. However, I'm not sure that this is guaranteed to happen
by the C++ standard; it might be implementation-defined.

--
Bob Hairgrove
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
alexandre.braganti@gmail.com
Guest
Posts: n/a
 
      03-10-2006
thanks for your answer, but I already thought to this issue : NO the
initInstance is NOT called in the constructor of the class... :-/

Any other idea ?


PS: i made a basic project with only 2 classes under Visual Studio in
order to check it again, and now I've not the same result at all : It
calls well ct_server::createList()... in my 'basic' project.

I'm becoming crazy

 
Reply With Quote
 
Jaspreet
Guest
Posts: n/a
 
      03-10-2006

Bob Hairgrove wrote:
> On 10 Mar 2006 06:13:00 -0800, (E-Mail Removed) wrote:
>
> >Hello,
> >First sorry for my poor English, I am French
> >I've got a comprehension problem of what happend in one of the project
> >i'm working on.
> >
> >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

>
> Where is ct_server::initInstance() called? If it is called in the
> constructor of the ct_server object, it is entirely possible that the
> base class version is called because the object has not yet finished
> construction. However, I'm not sure that this is guaranteed to happen
> by the C++ standard; it might be implementation-defined.
>
> --
> Bob Hairgrove
> (E-Mail Removed)


I guess calling a virtual function in the constructor is undefined
behaviour. That is because the vtbl is still not fully constructed by
that time.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-10-2006
* Bob Hairgrove:
> On 10 Mar 2006 06:13:00 -0800, (E-Mail Removed) wrote:
>
>> Hello,
>> First sorry for my poor English, I am French
>> I've got a comprehension problem of what happend in one of the project
>> i'm working on.
>>
>> 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

>
> Where is ct_server::initInstance() called? If it is called in the
> constructor of the ct_server object, it is entirely possible that the
> base class version is called because the object has not yet finished
> construction. However, I'm not sure that this is guaranteed to happen
> by the C++ standard; it might be implementation-defined.


It's guaranteed by the standard, and it's in the FAQ somewhere, and I
think your explanation is the most likely for the OP's case.

But anyway the programmer made an error: casting the this-pointer does
not affect which implementation is executed for a virtual function.

There's also a FAQ for the case where one really does want derived class
defined behavior during construction of a base class, called Dynamic
Binding During Construction (or some such, I don't exactly recall, since
I wanted to call it "virtual construction", but that term was already
used to denote what I'd want to call "cloning" -- oh well).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-10-2006
* Jaspreet:
>
> I guess calling a virtual function in the constructor is undefined
> behaviour. That is because the vtbl is still not fully constructed by
> that time.


Nope.

Check the FAQ.

Or the nearest textbook.



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-10-2006
(E-Mail Removed) wrote:
> thanks for your answer, but I already thought to this issue : NO the
> initInstance is NOT called in the constructor of the class... :-/
>
> Any other idea ?
>
>
> PS: i made a basic project with only 2 classes under Visual Studio in
> order to check it again, and now I've not the same result at all : It
> calls well ct_server::createList()... in my 'basic' project.
>
> I'm becoming crazy


Maybe. This program:
----------------------------------
#include <iostream>
using namespace std;

struct A {
virtual void foo(int i) {
cout << "A::foo(" << i << ")\n";
}
};

struct B : A {
virtual void foo(int i) {
cout << "B::foo(" << i << ")\n";
}
void bar(int i) {
(static_cast<A*>(this))->foo(i);
this->A::foo(i);
}
};

int main() {
B b;
b.bar(42);
}
----------------------------------
Should output:
B:42)
A:42)

And it does with VC++ v7.1 and VC++ v8 (and I stopped checking after
those). You must be (hopefully unintentionally) providing incorrect
information to us or collecting it from your colleague. Revisit your
own code.

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
alexandre.braganti@gmail.com
Guest
Posts: n/a
 
      03-10-2006
anyway, it's not a construction problem as my function is called
largely after all my objects have been fully constructed. Any other
idea ?

 
Reply With Quote
 
awm129@psualum.com
Guest
Posts: n/a
 
      03-10-2006
let me make sure I understand your question... you want to know why

(static_cast<GS_object*>(this))->createList();

calls createList() in the gs_object class, correct?

Virtual function calls all depend on the *run time* type of the object.
Your team mate is changing the run time type of the 'this' pointer
before making the call to createList. By casting the 'this' pointer to
a GS_object pointer, the 'this' no longer points to the ct_server
object, it now points to the gs_object object from which it was derived
and only has knowlege of functions within the gs_object class. This is
why then calling createList calls the createList function in the base
class.



The method you suggested:

gs_object::createList();

is also correct. This line translates into:

this -> gs_object::createList();

In this case, the correct function is determined by the compiler at
*compile time*. The 'this' pointer in this case still refers to an
object of type gs_object and you specify exactly which createList()
function to call. In my option, this method is preferable. Recasting
a this pointer can lead to strange behavior later in the function. It
can also cause strange run time bugs.


Hope this cleared things up for ya.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-10-2006
(E-Mail Removed) wrote:
> anyway, it's not a construction problem as my function is called
> largely after all my objects have been fully constructed. Any other
> idea ?


Go back to your project and find out more. Your explanation didn't hold
water.

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual functions in virtual base class sreelakshmi.rajula@gmail.com C++ 7 08-28-2006 09:29 PM
How to prevent a function in base class being overloaded from child class modemer C++ 20 03-22-2005 04:51 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM
Virtual functions and virtual base classes - I'm confused Michael Winter C++ 9 09-23-2003 11:17 PM



Advertisments