Why normal virtual function required blank body in base class
-
Hi, I have following block of code,
class Base
{
public:
Base()
{
cout<<"Constructor: Base"<<endl;
}
~Base()
{
cout<<"Destructor : Base"<<endl;
}
virtual void fun();};
class Derived: public Base
{public: Derived() { cout<<"Constructor: Derived"<<endl; } ~Derived() { cout<<"Destructor : Derived"<<endl; } void fun() { cout<<" in der"; }
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived obj;
obj.fun();
getche();In above code virtual fun() is showing error that it not resolved. If i add blank body then Derived class's Fun()is called. Is it necessary to have blank body for virtual function in base class and what is reason of this. :omg: Thanks abm
-
Hi, I have following block of code,
class Base
{
public:
Base()
{
cout<<"Constructor: Base"<<endl;
}
~Base()
{
cout<<"Destructor : Base"<<endl;
}
virtual void fun();};
class Derived: public Base
{public: Derived() { cout<<"Constructor: Derived"<<endl; } ~Derived() { cout<<"Destructor : Derived"<<endl; } void fun() { cout<<" in der"; }
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived obj;
obj.fun();
getche();In above code virtual fun() is showing error that it not resolved. If i add blank body then Derived class's Fun()is called. Is it necessary to have blank body for virtual function in base class and what is reason of this. :omg: Thanks abm
Hi, Your
Base::fun()
member is declared but not defined, so the linker complains. You have to define it somewhere either as pure virtualvirtual void fun() = 0;
or otherwise://do nothing
virtual void Base::fun(){};cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)