473,804 Members | 2,024 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
20 4964
* al************* ***@gmail.com:
anyway, it's not a construction problem as my function is called
largely after all my objects have been fully constructed. Any other
idea ?


What does 'largely' mean?

--
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 #11
* aw****@psualum. com:
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.
Right.

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.


Wrong.
--
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 #12
> 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?
Yes, that's it.
Virtual function calls all depend on the *run time* type of the object.
I agree.
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.


I do NOT agree : if I do something like this :

GSObject* myObj = (GSOject*) myCtServer
myObj->createList() ;

.... will call ct_server::crea teList altought myObj is a pointer on a
GSObject object. That why someone invented virtuals functions ;-)
am I right ?

So, according to me,
(static_cast<GS _object*>(this) )->createList() ;
is equivalent to :
GSObject* myObj = (GSOject*) myCtServer
myObj->createList() ;

In that case (static_cast<GS _object*>(this) )->createList() ; should call
ct_server::crea teList') and not gsobject::creat eList().

That the way I see it. That's NOT the way it works in my project :-/

Anyway, another basic project I've made (like the 'foo/bar' one of
Victor) give me all expected results.

That's why I don't understand what's happening with my original project
at all...

Mar 10 '06 #13
al************* ***@gmail.com wrote:
[..]
That's why I don't understand what's happening with my original project
at all...


Neither do we, since we aren't exposed to your original project. It's up
to you to solve it unless you want to post the entire project.

Start removing irrelevant code (or code that you think is irrelevant) and
see if the behaviour is still there. If you arrive at the same two-class
project, like in my example, and it still exhibits the "wrong" behaviour,
post it. I'll bet that the error is elsewhere, and by the time you get to
the minimal code that reproduces the problem, you will have solved the
mystery.

V
--
Please remove capital As from my address when replying by mail
Mar 10 '06 #14
Well I'm all wet. Thats what I get for relying on microsoft's
definition of static_cast before actually trying it. I ran something
similar to the example above and my results differed from what the
original post was describing. So, as I'm obviously wrong, can someone
explain why the static_cast doesn't work? Acording to microsoft:

http://msdn.microsoft.com/library/de...express_74.asp

It should work for what this guy intended. And try to avoid
explainations similar to "Wrong." thanks!

Mar 10 '06 #15
It's not so easy to remove irrelevant things in my project, or to post
it as it's a commercial project of more than 200 000 lines of code.

That's why i decided to post in this group : maybe someone else has
already had that kind of behavior and could tell me more about what's
happening...

Mar 10 '06 #16
al************* ***@gmail.com wrote:
It's not so easy to remove irrelevant things in my project, or to post
it as it's a commercial project of more than 200 000 lines of code.
Nobody said the life was easy.
That's why i decided to post in this group : maybe someone else has
already had that kind of behavior and could tell me more about what's
happening...


Your investigation about what's happening and why seems to be incomplete.

You claim that your colleague made some particular change that allegedly
caused a call to a virtual function to be resolved non-polymorphically .
Since it is well-known (by you as well) that such change should not affect
the outcome in the way claimed, the only two explanations are (a) the call
is [now] made from the constructor or destructor and (b) there is
a serious bug in the compiler. Since you have dismissed the former, and
the latter is _extremely_ unlikely, the only logical conclusion is that
you're mistaken and either your explanation is incorrect or your dismissal
of (a) is unfounded. That's why I said what I said, go back to your
project and look at it again, paying more attention. Perhaps the function
being called is _not_ the one that is overridden in the derived class due
to the difference in the arguments or some such. Perhaps the static_cast
is not the only difference between the project before and after it started
working "correctly" .

The ball is in your court. We cannot do anything without seeing the code.
All our guesses are a waste of time.

V
--
Please remove capital As from my address when replying by mail
Mar 10 '06 #17
aw****@psualum. com wrote:
Well I'm all wet. Thats what I get for relying on microsoft's
definition of static_cast before actually trying it. I ran something
similar to the example above and my results differed from what the
original post was describing. So, as I'm obviously wrong, can someone
explain why the static_cast doesn't work? Acording to microsoft:

http://msdn.microsoft.com/library/de...express_74.asp

It should work for what this guy intended.
No.

What in the explanation on MSDN leads you to believe that? I don't see
anything different on MSDN from how 'static_cast' is supposed to work (if
you look in the Standard).
And try to avoid
explainations similar to "Wrong." thanks!


Wrong. You're welcome!

V
--
Please remove capital As from my address when replying by mail
Mar 10 '06 #18

al************* ***@gmail.com wrote:
That's why i decided to post in this group : maybe someone else has
already had that kind of behavior and could tell me more about what's
happening...


Just a far fetched idea: Maybe this function exists in more versions
because you are using function overloading. If you have an bug there
this might cause the polymorphism seeming to fail...

Regards,
Matthias

Mar 10 '06 #19

aw****@psualum. com wrote:
Well I'm all wet. Thats what I get for relying on microsoft's
definition of static_cast before actually trying it.


A cast changes the type of the pointer. But this does not have impact
on which virtual function is actually being called. That's what virtual
functions/polymorphism is all about...

Regards,
Matthias

Mar 10 '06 #20

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

Similar topics

6
7590
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
4372
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
13061
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
3511
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
4230
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
2481
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
3554
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
2710
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
9711
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
10595
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
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
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...
1
7633
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
6862
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
5529
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...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
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.