Or we can let Base/Derived manage class A on their own:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::ostream;
//------------------------------------------------
class A
{
public:
A(){}
vector<string>& model_lookupTable()
{
return ModelLookupTable_;
}
private:
vector<string> ModelLookupTable_;
};
//------------------------------------------------
class Base
{
public:
Base() : theA(new A)
{
theA->model_lookupTable().push_back("Base") ;
}
~Base()
{
delete theA;
}
//
virtual void some_function()=0;
//
const vector<string>& get_table()
{
return theA->model_lookupTable();
}
protected:
A* theA;
};
//------------------------------------------------
class Derived : public Base
{
public:
Derived(){}
void some_function()
{
theA->model_lookupTable().push_back("Derived");
}
};
//------------------------------------------------
int main()
{
//1) Constructor of class Base will create an instance of class A.
//2) Then it will modify the property of class A as a demo.
Base* myInstance = new Derived;
//Constructor of class Derived will modify property of class A. Class A's
//instance is created by class Base.
myInstance->some_function();
//So what do we got for myInstance?
for(unsigned i = 0; i < myInstance->get_table().size(); ++i)
{
cout<<"From instance: "<< myInstance->get_table()[i]<<endl;
}
delete myInstance;
//A's instance cease to exist after deletion of myInstance.
return 0;
}