473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1478
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
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...
19
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
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...
24
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
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...
17
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...
17
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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,...

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.