473,666 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

when EXACTLY is virtual mechanism used?

struct CBase
{ virtual inline void foo() { puts("base"); }
};
void main()
{ CBase obj;
(&obj)->foo(); // static call or
} // dynamic (using virtual table) call ?

Is it compiler dependend, or does the standart say anything about
wheter the static or the dynamic call is used? With Visual C++ 5.0,
the call is static.

I am asking because I thought that dynamic calls are used in
connection with pointers or references, and (&obj) is a pointer. I
know that the compiler can be smart enough to see that the dynamic
type of the pointer (&obj) will be CBase, and thus use the more
efficient static call.

But I'm curious anyway. What does the standart say about it? In
exactly which cases is the call dynamic?

Thank you for your help

Sensorflo
Jul 22 '05 #1
3 1492
Sensorflo wrote:
struct CBase
{ virtual inline void foo() { puts("base"); }
};
void main()
void main coats your mouse with a pasty film. Use int main.
{ CBase obj;
(&obj)->foo(); // static call or
} // dynamic (using virtual table) call ?

Is it compiler dependend, or does the standart say anything about
wheter the static or the dynamic call is used? With Visual C++ 5.0,
the call is static.


How do you know the call is static?

If you did not disassemble, or whatever you did, how could you have known?

C++ compilers may optimize anything in any way they can, so long as a
well-formed and well-defined program works "as if" the compiler had followed
the Standard's ideal machine rules.

Using a pointer (or an address, in your case) won't force a dynamic call,
because in this case the compiler can optimize away the dereferrence, get
back to the original object, see it is not derived, and call its method
directly. This probably happens in a "virtual code" layer, between the raw
C++ source and the raw machine language output, which is itself unaware it
is resolving a virtual call. That layer only sees idealized opcodes, and
then optimizes them.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #2
Sensorflo wrote:
struct CBase
{ virtual inline void foo() { puts("base"); }
};
void main()
{ CBase obj;
(&obj)->foo(); // static call or
} // dynamic (using virtual table) call ?

Is it compiler dependend, or does the standart say anything about
wheter the static or the dynamic call is used? With Visual C++ 5.0,
the call is static.


Compiler is free to optimize virtual method call if it can find the
exact dynamic type of the object. The C++ standard does not specify the
way of invocation of virtual functions. It just says that it should work
depending on the dynamic object type (C++ std 10.3/6).

The virtual call mechanism would be used if no hint in the entire
compilation unit was given about the dynamic type of the object.

Something like:

// somefile.cpp

#include "CBase.h"

void bar (Cbase* obj)
{
obj->foo(); // No idea if obj is of type CBase or derived.
}
Regards,
Janusz

Jul 22 '05 #3
Sensorflo wrote:
struct CBase
{ virtual inline void foo() { puts("base"); }
};
void main()
{ CBase obj;
(&obj)->foo(); // static call or
} // dynamic (using virtual table) call ?

Is it compiler dependend, or does the standart say anything about
wheter the static or the dynamic call is used? With Visual C++ 5.0,
the call is static.
The standard doesn't say anything about any "static" or "dynamic" calls.
When it comes to non-qualified calls, the standard simply says that when
you call a non-virtual member function, the concrete function is chosen
in accordance with _static_ type of the object expression. When you call
a virtual member function, the concrete function is chosen in accordance
with _dynamic_ type of the object expression. That's all there is to it.

What steps compilers take in order to satisfy this requirement is
completely up to their implementers. They may decide to use dynamic
calls for _all_ member function calls, as long as the above requirements
are met (and vice versa). If in some situation they are sure that they
can correctly resolve a call to virtual function without using a dynamic
call - they are free to do so.
I am asking because I thought that dynamic calls are used in
connection with pointers or references, and (&obj) is a pointer. I
know that the compiler can be smart enough to see that the dynamic
type of the pointer (&obj) will be CBase, and thus use the more
efficient static call.
Yes, that's entirely possible. It depends on the compiler's capabilities
to detect such situations.
But I'm curious anyway. What does the standart say about it? In
exactly which cases is the call dynamic?


The standard doesn't go into implementation details in this case.
There's no reason for it to do so.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

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

Similar topics

2
33173
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used to work before I added the changes in, and I'm not sure if that's causing it.
19
2333
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
15
10628
by: Dave Townsend | last post by:
Yo, I had a job interview today, the interviewing asked me about inline virtual functions, or what was my opinion on them. Hm, I've seen mention of these babies in the reference material, but I've never used one. ( I'm an experienced software developer and have used C++ for more than 10 years) Anybody found the use of one of these guys necessary or useful. Curious minds need to know for the next curve ball question coming my
24
2755
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
26
3971
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
17
14706
by: ypjofficial | last post by:
Hello All, I have read in many c++ literature that vtable is nothing but an array of pointer to virtual functions inside a class.And the class where the virtual function/s are declared stores the vfptr i.e the pointer to vtable internally. What confuses me is if vtable is an array of pointer to virtual functions then as per the properties of the array all the entries inside the array must be same.i.e all the pointers should be of similar...
17
1431
by: Michael | last post by:
Hi, Could you pleaes let me know when I need to use virtual destctor function in the base class? Thanks in advance, Michael
6
3221
by: Gerhard Prilmeier | last post by:
Hello, I have an unmanaged C++ API that uses virtual functions, like this: class A { public: virtual void handleMe(){} };
23
4599
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
8876
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
8784
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...
1
8556
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
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
6198
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
4198
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
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
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
2011
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.