473,796 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual overloaded functions and base class function call...

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::init Instance, one boy of my team wanted to
call the gs_object::crea teList() base function, and not the overloaded
one (ct_server::cre ateList() ). But, according to me he made a mistake
as he wrote :

(static_cast<GS _object*>(this) )->createList() ;

instead of

gs_object::crea teList();
According to me, as createList() is virtual, this line of code should
call ct_server::crea teList and not gs_object::crea teList()

But it doesn't : when in run in debug mode, i can see it calls
gs_object::crea teList();

I can't understand why. Could you explain me ?
FYI, i'm using Visual C++ 7.1.3 ; Qt 3.3.4

Mar 10 '06 #1
20 4963
On 10 Mar 2006 06:13:00 -0800, al************* ***@gmail.com 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::init Instance, one boy of my team wanted to
call the gs_object::crea teList() base function, and not the overloaded
one (ct_server::cre ateList() ). But, according to me he made a mistake
as he wrote :

(static_cast<G S_object*>(this ))->createList() ;

instead of

gs_object::cre ateList();
According to me, as createList() is virtual, this line of code should
call ct_server::crea teList and not gs_object::crea teList()

But it doesn't : when in run in debug mode, i can see it calls
gs_object::cre ateList();

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::init Instance() 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
No**********@Ho me.com
Mar 10 '06 #2
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::crea teList()... in my 'basic' project.

I'm becoming crazy ;-)

Mar 10 '06 #3

Bob Hairgrove wrote:
On 10 Mar 2006 06:13:00 -0800, al************* ***@gmail.com 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::init Instance, one boy of my team wanted to
call the gs_object::crea teList() base function, and not the overloaded
one (ct_server::cre ateList() ). But, according to me he made a mistake
as he wrote :

(static_cast<G S_object*>(this ))->createList() ;

instead of

gs_object::cre ateList();
According to me, as createList() is virtual, this line of code should
call ct_server::crea teList and not gs_object::crea teList()

But it doesn't : when in run in debug mode, i can see it calls
gs_object::cre ateList();

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::init Instance() 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
No**********@Ho me.com


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.

Mar 10 '06 #4
* Bob Hairgrove:
On 10 Mar 2006 06:13:00 -0800, al************* ***@gmail.com 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::init Instance, one boy of my team wanted to
call the gs_object::crea teList() base function, and not the overloaded
one (ct_server::cre ateList() ). But, according to me he made a mistake
as he wrote :

(static_cast<GS _object*>(this) )->createList() ;

instead of

gs_object::crea teList();
According to me, as createList() is virtual, this line of code should
call ct_server::crea teList and not gs_object::crea teList()

But it doesn't : when in run in debug mode, i can see it calls
gs_object::crea teList();

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::init Instance() 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?
Mar 10 '06 #5
* 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?
Mar 10 '06 #6
anyway, it's not a construction problem as my function is called
largely after all my objects have been fully constructed. Any other
idea ?

Mar 10 '06 #7
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::crea teList();

is also correct. This line translates into:

this -> gs_object::crea teList();

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.

Mar 10 '06 #8
al************* ***@gmail.com 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::crea teList()... 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
Mar 10 '06 #9
al************* ***@gmail.com 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
Mar 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7589
by: Vajira | last post by:
Hello, Can you tell me why compilar does not recognize base class's virtual function in the following code? Is there is any limitation in C++, related to overloading virtual function of a base class in the derived class ? Compilar printed following message > g++ main2.cpp main2.cpp: In function `int main()': main2.cpp:21: no matching function for call to `B::Display(int)'
11
4369
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
9
13060
by: kish_nand | last post by:
Could someone please explain me the concept behind virtual functions and vtables. I am little confused about this. Please refer to the following code and tell me how many virtual tables would be created and what they would contain: class base { virtual void display() { cout<<"base display"<<endl;
1
6652
by: ravinderthakur | last post by:
hi all experts, i was just thinking about making overloaded operators virtual. i was wordering what could be the implications of such scenarios. i will be thankful if somebody can provide point out some general issues in making overloaded virtual or point me to some resource having explaination about the same.
15
3508
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
2479
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it has nothing to do with making multiple arguments in a declaration like:
17
3551
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
7
2709
by: Mark | last post by:
Hi, I have an abstract class which I want my other classes to inherit from. In the constructor of the abstract class I want to check if certain virtual functions have been overloaded or not. Is this possible? For example, let's say I have class Base {
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.