虚构造函数

使用工厂方法模式的主要目标之一就是更好地组织代码,使得在创建对象时不需要选择准确的构造函数类型。也就是说,可以告诉工厂:“现在还不能确切地知道需要什么类型的对象,但是这里有一些信息。请创建类型适当的对象。”

此外,在构造函数调用期间,虚拟机制并不起作用(发生早期绑定)。在某些情况下这是很棘手的事情。例如,在Shape程序中,在Shape对象的构造函数内部建立一既需要的东西然后由draw()给制Shape,这似乎是合理的。函数draw()应该是一个虚函数,它将根据传递给Shape的消息绘制相应的图形,消息表明图形本身是Circle、Square或者Line。然而,这些操作在构造函数内部不能采用这种方法,因为当在构造函数内部调用虚函数时,将由虚函数决定指向哪个“局部的”函数体。

可是,程序员有时还想要得到接近于虚构造函数的行为。

Coplien将他给出的解决问题的方法取名为“信封和信件类”。“信封”类是基类,它是一个包含指向一个对象的指针的外壳,该对象也是一个基类类型。“信封”类的构造函数决定采用什么样的特定类型,在堆上创建一个该类型的对象,然后对他的指针分配对象(决定是在运行中调用构造函数时做出的,而不是在编译中做类型检查时做出的)。随后的所有函数调用都是由基类通过它指针来进行处理。这实际上就是状态模式的小小变形,其中基类扮演派生类的代理角色,而派生类提供行为中的变化。

#include <iostream>
#include <string>
#include <stdexcept>
#include <stdexcept>
#include <cstddef>
#include <vector>
//#include "../purge.h"
using namespace std;

class Shape {
	Shape* s;
	// Prevent copy-construction & operator=
	Shape(Shape&);
	Shape operator=(Shape&);
protected:
	Shape() { s = 0; }
public:
	virtual void draw() { s->draw(); }
	virtual void erase() { s->erase(); }
	virtual void test() { s->test(); }
	virtual ~Shape() {
		cout << "~Shape" << endl;
		if(s) {
			cout << "Making virtual call: ";
			s->erase(); // Virtual call
		}
		cout << "delete s: ";
		delete s; // The polymorphic deletion
		// (delete 0 is legal; it produces a no-op)
	}
	class BadShapeCreation : public logic_error {
	public:
		BadShapeCreation(string type)
			: logic_error("Cannot create type " + type) {}
	};
	Shape(string type) throw(BadShapeCreation);
};

class Circle : public Shape {
	Circle(Circle&);
	Circle operator=(Circle&);
	Circle() {} // Private constructor
	friend class Shape;
public:
	void draw() { cout << "Circle::draw" << endl; }
	void erase() { cout << "Circle::erase" << endl; }
	void test() { draw(); }
	~Circle() { cout << "Circle::~Circle" << endl; }
};

class Square : public Shape {
	Square(Square&);
	Square operator=(Square&);
	Square() {}
	friend class Shape;
public:
	void draw() { cout << "Square::draw" << endl; }
	void erase() { cout << "Square::erase" << endl; }
	void test() { draw(); }
	~Square() { cout << "Square::~Square" << endl; }
};

Shape::Shape(string type) throw(Shape::BadShapeCreation) {
	if(type == "Circle")
		s = new Circle;
	else if(type == "Square")
		s = new Square;
	else throw BadShapeCreation(type);
	draw();  // Virtual call in the constructor
}

char* sl[] = { "Circle", "Square", "Square",
	"Circle", "Circle", "Circle", "Square" };

int main() {
	vector<Shape*> shapes;
	cout << "virtual constructor calls:" << endl;
	try {
		for(size_t i = 0; i < sizeof sl / sizeof sl[0]; i++)
			shapes.push_back(new Shape(sl[i]));
	} catch(Shape::BadShapeCreation e) {
		cout << e.what() << endl;
		//purge(shapes);
		return EXIT_FAILURE;
	}
	for(size_t i = 0; i < shapes.size(); i++) {
		shapes[i]->draw();
		cout << "test" << endl;
		shapes[i]->test();
		cout << "end test" << endl;
		shapes[i]->erase();
	}
	Shape c("Circle"); // Create on the stack
	cout << "destructor calls:" << endl;
	//purge(shapes);
}

虚构造函数Shape(type)在所有派生类未声明前不能定义。

选自《C++编程思想》。

原文链接: https://www.cnblogs.com/chinaxmly/archive/2012/10/02/2710339.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    虚构造函数

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/64666

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午11:29
下一篇 2023年2月9日 上午11:29

相关推荐