when EXACTLY is virtual mechanism used?

S

Sensorflo

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
 
P

Phlip

Sensorflo said:
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.
 
J

Janusz Szpilewski

Sensorflo said:
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
 
A

Andrey Tarasevich

Sensorflo said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top