sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
dc's Avatar

Virtual keyword & compile time


Question posted by: dc (Guest) on January 11th, 2006 09:35 AM
Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time

7 Answers Posted
Neelesh Bodas's Avatar
Guest - n/a Posts
#2: Re: Virtual keyword & compile time


dc wrote:[color=blue]
> Can anybody think of a situation where virtual function's address
> resolution/ something related to virtual is done at compile time[/color]

1. Virtual functions called inside constructor and destructor are
statically bound.
2. If a virtual function is called on the _object_ instead of calling
on _pointer_ or _reference_, it is statically bound.

Of course, the fact that it is statically bound has nothing to do with
the resolution being done at compile time, but smart compilers can take
advantage of this and actually do the resolution at compile time.

Rolf Magnus's Avatar
Guest - n/a Posts
#3: Re: Virtual keyword & compile time

Neelesh Bodas wrote:
[color=blue]
>
> dc wrote:[color=green]
>> Can anybody think of a situation where virtual function's address
>> resolution/ something related to virtual is done at compile time[/color]
>
> 1. Virtual functions called inside constructor and destructor are
> statically bound.[/color]

AFAIK, they are still dynamically bound, but only up to the class that the
constructor/destructor belongs to. The effect is the same for functions
called directly, but if called indirectly (i.e. from a member function that
is called from the constructor or destructor), there is a difference.

Btw: Is there a common word for both constructors and destructors so that
one doesn't have to mention both each time? I once read "structors"
somewhere, but it looks strange to me and I'm not sure it's widely
understood.

Neelesh Bodas's Avatar
Guest - n/a Posts
#4: Re: Virtual keyword & compile time


Rolf Magnus wrote:[color=blue]
> Neelesh Bodas wrote:
>[color=green]
> >
> > dc wrote:[color=darkred]
> >> Can anybody think of a situation where virtual function's address
> >> resolution/ something related to virtual is done at compile time[/color]
> >
> > 1. Virtual functions called inside constructor and destructor are
> > statically bound.[/color]
>
> AFAIK, they are still dynamically bound, but only up to the class that the
> constructor/destructor belongs to. The effect is the same for functions
> called directly, but if called indirectly (i.e. from a member function that
> is called from the constructor or destructor), there is a difference.
>[/color]

Rereading my post after your reply, I think that I used completely
wrong words - "statically bound" , since by definition, "statically
bound" means resolved at compile time whereas dynamically "bound means"
resolved at run time -- (Please correct me if I am wrong).

I wanted to say that the virtual function (for the same object ) called
inside the constructor or destructors (the "structors") are bound
locally - ie. the version of the same class. Similar case is with the
virtual functions being called on object rather than a pointer or
referece.

Please correct me in case thats not what actually happens.

Rolf Magnus's Avatar
Guest - n/a Posts
#5: Re: Virtual keyword & compile time

Neelesh Bodas wrote:
[color=blue][color=green][color=darkred]
>> > dc wrote:
>> >> Can anybody think of a situation where virtual function's address
>> >> resolution/ something related to virtual is done at compile time
>> >
>> > 1. Virtual functions called inside constructor and destructor are
>> > statically bound.[/color]
>>
>> AFAIK, they are still dynamically bound, but only up to the class that
>> the constructor/destructor belongs to. The effect is the same for
>> functions called directly, but if called indirectly (i.e. from a member
>> function that is called from the constructor or destructor), there is a
>> difference.
>>[/color]
>
> Rereading my post after your reply, I think that I used completely
> wrong words - "statically bound" , since by definition, "statically
> bound" means resolved at compile time whereas dynamically "bound means"
> resolved at run time -- (Please correct me if I am wrong).
>
> I wanted to say that the virtual function (for the same object ) called
> inside the constructor or destructors (the "structors") are bound
> locally - ie. the version of the same class. Similar case is with the
> virtual functions being called on object rather than a pointer or
> referece.
>
> Please correct me in case thats not what actually happens.[/color]

Well, as I said, for functions called directly from within the constructor
or destructor, the effect is the same. But consider this example:

#include <iostream>

struct Base
{
virtual void hello()
{
std::cout << "Hello, world, I'm a Base\n";
}

void test()
{
hello();
}
};

struct Derived
{
virtual void hello()
{
std::cout << "This is Derived saying Hello\n";
}

Derived()
{
test();
}
};

int main()
{
Derived();
}

Of course, you can extend that example to a bigger hierarchy. The bottom
line is that the virtual functions get indeed resolved dynamically, but
only up to the class that the constructor belongs to. If Base::test() gets
inlined, the compiler has a good chance of resolving the call at compile
time, but otherwise it must defer it until runtime.

Rolf Magnus's Avatar
Guest - n/a Posts
#6: Re: Virtual keyword & compile time

Ok, forgot to test the code before posting, and of course, I forgot
something.

Rolf Magnus wrote:
[color=blue]
> #include <iostream>
>
> struct Base
> {
> virtual void hello()
> {
> std::cout << "Hello, world, I'm a Base\n";
> }
>
> void test()
> {
> hello();
> }
> };
>
> struct Derived[/color]

: public Base
[color=blue]
> {
> virtual void hello()
> {
> std::cout << "This is Derived saying Hello\n";
> }
>
> Derived()
> {
> test();
> }
> };
>
> int main()
> {
> Derived();
> }[/color]


W Marsh's Avatar
Guest - n/a Posts
#7: Re: Virtual keyword & compile time

On Wed, 11 Jan 2006 15:53:15 +0100, Rolf Magnus <ramagnus@t-online.de>
wrote:
[color=blue]
>Well, as I said, for functions called directly from within the constructor
>or destructor, the effect is the same. But consider this example:
>
>#include <iostream>
>
>struct Base
>{
> virtual void hello()
> {
> std::cout << "Hello, world, I'm a Base\n";
> }
>
> void test()
> {
> hello();
> }
>};
>
>struct Derived
>{
> virtual void hello()
> {
> std::cout << "This is Derived saying Hello\n";
> }
>
> Derived()
> {
> test();
> }
>};
>
>int main()
>{
> Derived();
>}[/color]

You forgot to inherit from Base in class Derived (oops!). For anybody
trying to learn from this code, it should actually be:

#include <iostream>

struct Base
{
virtual void hello()
{
std::cout << "Hello, world, I'm a Base\n";
}

void test()
{
hello();
}
};

struct Derived : public Base
{
virtual void hello()
{
std::cout << "This is Derived saying Hello\n";
}

Derived()
{
test();
}
};

int main()
{
Derived();
}
dc's Avatar
Guest - n/a Posts
#8: Re: Virtual keyword & compile time

Thanks all for ur inputs.

That means virtual functions are never resolved during compile time
even when called directly from with in constructor/destructor or
invoked with object ( ie neither ptr nor reference.)

Plz correct if I am wrong..

 
Not the answer you were looking for? Post your question . . .
197,000 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,000 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors