Possible Duplicate:
Why does an overridden function in the derived class hide other overloads of the base class?
Hi,
Let me explain my question with this example :
class A
{
virtual ~A() = 0 { }
A(const A&);
virtual void operator =(const A&) = 0;
}
class B : public A
{
~B();
B(const B&);
void operator =(const B&);
}
void main(void)
{
A* a = new B();
delete a; // Is ~A() called, ~B() or both ?
}
This brings me to ask two questions :
- Which destructor is called when using delete on an abstract-base-class pointer ?
- Is it possible to make a copy of my object “a” with one of the copy methods above ?
Both are called.
In
delete athe derived class destructor~B::Bis found and called using the virtual dispatch mechanism. The implementation of~B::Bin turn implicitly calls the base class destructor~A::A. However, this call is implemented as non-virtual ordinary call, meaning it is not affected by the fact that~A::Ais declared as pure.This is actually the reason why pure virtual destructors still have to be defined. Although the language specification does not permit in-class definitions for pure virtual functions. Even if you want it to be inline, you still have to define it out of class.
It is not possible to copy a standalone object of type
Asimply because there can’t be any standalone object of abstract type. Please, clarify your question. What do you want to copy and to where?If you want to do something like this
and expect the last assignment to behave as if
b1 = b2was performed… well, it can be done but it will take some effort and might/will result in pretty ugly code. I’d say that burdening the overloaded assignment operator with that sort of functionality is not a good idea.