Virtual keyword & compile time

D

dc

Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time
 
N

Neelesh Bodas

dc said:
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.
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.
 
R

Rolf Magnus

Neelesh said:
1. Virtual functions called inside constructor and destructor are
statically bound.

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.
 
N

Neelesh Bodas

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

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.
 
R

Rolf Magnus

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

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.
 
R

Rolf Magnus

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

Rolf said:
#include <iostream>

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

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

struct Derived

: public Base
 
W

W Marsh

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();
}

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();
}
 
D

dc

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..
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top