D
dc
Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time
resolution/ something related to virtual is done at compile time
dc said:Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time
Neelesh said:1. Virtual functions called inside constructor and destructor are
statically bound.
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.
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.
Rolf said:#include <iostream>
struct Base
{
virtual void hello()
{
std::cout << "Hello, world, I'm a Base\n";
}
void test()
{
hello();
}
};
struct Derived
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();
}
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.