赞
踩

要回答这个问题,首先需要弄清楚的问题是,在C++中类的构造函数、析构函数的调用次序是什么?
这里指假设基类和派生类的成员中,包含其他类的对象。
在C++中,构造函数的调用次序是:
基类的成员->基类的构造函数体->派生类的成员->派生类的构造函数体
析构函数的调用次序是:
派生类的析构函数->派生类的成员析构->基类的析构函数->基类的成员析构
#include <iostream> using namespace std; class AA { public: AA() { cout<<"AA Construct"<<endl; } ~AA() { cout<<"AA Destroy"<<endl; } }; class BB { public: BB() { cout<<"BB Construct"<<endl; } ~BB() { cout<<"BB Destroy"<<endl; } }; class Base { public: Base() { cout<<"Base Construct"<<endl; } ~Base() { cout<<"Base Destroy"<<endl; } private: AA aa; }; class Derive: public Base { public: Derive() { cout<<"Derive Construct"<<endl; } ~Derive() { cout<<"Derive Destroy"<<endl; } private: BB bb; }; int main() { Base *p = new Derive; delete p; /*运行结果 AA Construct Base Construct BB Construct Derive Construct Base Destroy AA Destroy */ }
从程序的运行情况来看,我们不难发现,在最后释放指针p所指向的对象时,只释放了基类的部分,没有释放派生类的部分。这是因为,指针p声明时是基类的指针,而基类的析构函数不是虚函数,所以调用那个析构函数是在编译时确定的。如果要执行正确的析构顺序,需要将基类的析构函数定义为 virtual, 这样派生类的析构函数就自动是virtual的了,在最后释放指针p时,按照RTTI,执行p所指真实对象的析构函数
//当类里面没有数据只有纯虚方法时称之为接口 class Shape // 接口 { public: Shape() { cout << "Shape" << endl; } virtual ~Shape() { cout << "~Shape" << endl; } virtual void draw() = 0; virtual void erase() = 0; }; class Circle : public Shape { public: Circle() { cout << "Circle" << endl; } virtual ~Circle() { cout << "~Circle" << endl; } void draw() { cout << "circle::draw" << endl; } void erase() { cout << "circle::erase" << endl; } }; class Square : public Shape { public: Square() { cout << "Square" << endl; } virtual ~Square() { cout << "~Square" << endl; } void draw() { cout << "Square::draw" << endl; } void erase() { cout << "Square::erase" << endl; } }; class ShapeFactory//工厂 { public: ShapeFactory() { cout << "ShapeFactory" << endl; } virtual ~ShapeFactory() { cout << "~ShapeFactory" << endl; } virtual Shape* create() = 0; }; class CircleFactory : public ShapeFactory { public: CircleFactory() { cout << "CircleFactory" << endl; } ~CircleFactory() { cout << "~CircleFactory" << endl; } virtual Shape* create() { return new Circle(); } }; class SquareFactory : public ShapeFactory { public: SquareFactory() { cout << "SquareFactory" << endl; } ~SquareFactory() { cout << "~SquareFactory" << endl; } virtual Shape* create() { return new Square(); } }; int main() { ShapeFactory* factory = new SquareFactory(); Shape* shape = factory->create(); shape->draw(); shape->erase(); factory = new CircleFactory(); shape = factory->create(); shape->draw(); shape->erase(); return 0; }

#include<iostream> #include<atomic> #include<string> #include<map> //key_value键值对,和值 using namespace std; //当类里面没有数据只有纯虚方法时称之为接口 class Shape // 接口 { public: Shape() { cout << "Shape" << endl; } virtual ~Shape() { cout << "~Shape" << endl; } virtual void draw() = 0; virtual void erase() = 0; }; class Circle : public Shape { public: Circle() { cout << "Circle" << endl; } virtual ~Circle() { cout << "~Circle" << endl; } void draw() { cout << "circle::draw" << endl; } void erase() { cout << "circle::erase" << endl; } }; class Square : public Shape { public: Square() { cout << "Square" << endl; } virtual ~Square() { cout << "~Square" << endl; } void draw() { cout << "Square::draw" << endl; } void erase() { cout << "Square::erase" << endl; } }; class ShapeFactory//工厂 { public: ShapeFactory() { cout << "ShapeFactory" << endl; } virtual ~ShapeFactory() { cout << "~ShapeFactory" << endl; } virtual Shape* create() = 0; public: static std::map<string, ShapeFactory*> factories; // key value // ShapeName; static Shape* createShape(const string& id) { if (factories.find(id) != factories.end()) { return factories[id]->create(); } else { return nullptr; } } }; //静态成员的初始化 std::map<string, ShapeFactory*> ShapeFactory::factories; class CircleFactroy : public ShapeFactory { public: CircleFactroy() { cout << "CircleFactroy" << endl; } ~CircleFactroy() { cout << "~CircleFactroy" << endl; } virtual Shape* create() { return new Circle(); } }; class SquareFactroy : public ShapeFactory { public: SquareFactroy() { cout << "SquareFactroy" << endl; } ~SquareFactroy() { cout << "~SquareFactroy" << endl; } virtual Shape* create() { return new Square(); } }; //形状工厂的初始化,主要负责初始map class ShapeFactoryInitializer {//不能在类里面定义一个类型成员会无限递归下去,使用static static ShapeFactoryInitializer si; ShapeFactoryInitializer() { // key value ShapeFactroy * ShapeFactory::factories["Circle"] = new CircleFactroy(); ShapeFactory::factories["Square"] = new SquareFactroy(); } }; //下面这行代码会在主函数之前生成CircleFactroy和SquareFactroy对象 ShapeFactoryInitializer ShapeFactoryInitializer::si; // Client int main() { Shape* shape = ShapeFactory::createShape("Circle"); shape->draw(); shape->erase(); shape = ShapeFactory::createShape("Square"); shape->draw(); shape->erase(); return 0; }

#include<iostream> #include<atomic> #include<string> #include<map> #include<memory> //当类里面没有数据只有纯虚方法时称之为接口 class Shape // 接口 { public: Shape() { cout << "Shape" << endl; } virtual ~Shape() { cout << "~Shape" << endl; } virtual void draw() = 0; virtual void erase() = 0; }; class Circle : public Shape { public: Circle() { cout << "Circle" << endl; } virtual ~Circle() { cout << "~Circle" << endl; } void draw() { cout << "circle::draw" << endl; } void erase() { cout << "circle::erase" << endl; } }; class Square : public Shape { public: Square() { cout << "Square" << endl; } virtual ~Square() { cout << "~Square" << endl; } void draw() { cout << "Square::draw" << endl; } void erase() { cout << "Square::erase" << endl; } }; class ShapeFactory//工厂 { public: ShapeFactory() { cout << "ShapeFactory" << endl; } virtual ~ShapeFactory() { cout << "~ShapeFactory" << endl; } virtual std::shared_ptr<Shape> create() = 0; public: static std::map<string, std::shared_ptr<ShapeFactory>> factories; // key value // ShapeName; static std::shared_ptr<Shape> createShape(const string& id) { if (factories.find(id) != factories.end()) { return factories[id]->create(); } else { return nullptr; } } }; //静态成员的初始化 std::map<string, std::shared_ptr<ShapeFactory>> ShapeFactory::factories; class CircleFactroy : public ShapeFactory { public: CircleFactroy() { cout << "CircleFactroy" << endl; } ~CircleFactroy() { cout << "~CircleFactroy" << endl; } virtual std::shared_ptr<Shape> create() { return std::make_shared<Circle>(); } }; class SquareFactroy : public ShapeFactory { public: SquareFactroy() { cout << "SquareFactroy" << endl; } ~SquareFactroy() { cout << "~SquareFactroy" << endl; } virtual std::shared_ptr<Shape> create() { return std::make_shared<Square>(); } }; //形状工厂的初始化,主要负责初始map class ShapeFactoryInitializer {//不能在类里面定义一个类型成员会无限递归下去,使用static static ShapeFactoryInitializer si; ShapeFactoryInitializer() { // key value ShapeFactroy * ShapeFactory::factories["Circle"] = std::make_shared<CircleFactroy>();//new CircleFactroy(); ShapeFactory::factories["Square"] = std::make_shared<SquareFactroy>();//new SquareFactroy(); } }; //下面这行代码会在主函数之前生成CircleFactroy和SquareFactroy对象 ShapeFactoryInitializer ShapeFactoryInitializer::si; // Client int main() { std::shared_ptr<Shape> shape = ShapeFactory::createShape("Circle"); shape->draw(); shape->erase(); shape = ShapeFactory::createShape("Square"); shape->draw(); shape->erase(); return 0; }






Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。