 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sensorflo Guest
|
Posted: Sat Jan 31, 2004 1:59 am Post subject: 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
|
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Sat Jan 31, 2004 2:05 am Post subject: Re: when EXACTLY is virtual mechanism used? |
|
|
Sensorflo wrote:
| Quote: | struct CBase
{ virtual inline void foo() { puts("base"); }
};
void main()
|
void main coats your mouse with a pasty film. Use int main.
| Quote: | { 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?TestFirstUserInterfaces
|
|
| Back to top |
|
 |
Janusz Szpilewski Guest
|
Posted: Sat Jan 31, 2004 2:26 am Post subject: Re: when EXACTLY is virtual mechanism used? |
|
|
Sensorflo wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Sat Jan 31, 2004 8:18 pm Post subject: Re: when EXACTLY is virtual mechanism used? |
|
|
Sensorflo wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|