#include <iostream>
using namespace std;
class Aggregated{
public:
Aggregated(){
cout<<"Aggregated construct"<<endl;
}
~Aggregated(){
cout<<"Aggregated destruct"<<endl;
}
};
class Base{
protected:
Aggregated * ptr;
public:
Base():ptr(NULL){
cout<<"Base construct"<<endl;
f();
}
virtual ~Base()=0;
virtual void f()=0;
};
Base::~Base(){
if(ptr)
delete ptr;
cout<<"Base destruct"<<endl;
}
void Base::f(){
cout<<"Base f"<<endl;
}
class Derived: public Base{
public:
Derived(){
if(!ptr)
ptr = new Aggregated;
cout<<"Derived construct"<<endl;
}
~Derived(){
cout<<"Derived destruct"<<endl;
}
void f(){}
};
int main(){
Base* b = new Derived();
delete b;
cin.get();
return 0;
}
DileriumL - my compilers show some warnings about running pure virtual, but run it successfully, since this pure virtual has a defined body. I checked it on few compilers
2022 Oct 15, 8:45:35 PM
There is actually runtime error. You can't call pure virtual function in constructor. Fix this, please
2022 Aug 23, 7:40:08 PM
Login in to like
Login in to comment