Suppose that I have a base class pointer, with base class having virtual functions. And if I am pointing a derived class object using base class pointer then how compiler knows that it is derived version of that virtual function to be called since at the time of assignment the derived class address would have been converted into base class type, why doesn't compiler generate an error since memory layout of both may be different.
And also if I have declared base class destructor virtual and deleting the base pointer having address of derived class then how does compiler know that I am actually deleting the derived object.
class a{
public:
virtual ~a(){}
virtual void f(){}
};
class b:public a
{
public:
~b(){}
void f(){ cout<<"hi";}
};
main()
{
a *aptr;
aptr=new b; //here aptr has the address of b;
delete aptr; //since pointer type is base class so how will compiler know that
//it has to call derived's destructor,also their names are different
//as one is a and another is b.
}
main()
without return type is acceptable C++); all that matters is that the standard says that it will work. – Kerrek SBmain
's return type there, Skippy. – Lightness Races in Orbit