如何在基类中声明类似工厂的方法? 我正在寻找C++类设计问题的解决方案。我试图实现的是在基类中使用静态方法,它将返回后代类型对象的实例。关键是,其中一些应该是单身。我在VCL中编写,所以有可能使用属性>代码>,但我更喜欢纯C++解决方案。 class Base { private: static Base *Instance; public: static Base *New(void); virtual bool isSingleton(void) = 0; } Base::Instance = NULL; class First : public Base { // singleton descendant public: bool isSingleton(void) { return true; } } class Second : public Base { // normal descendant public: bool isSingleton(void) { return false; } } Base *Base::New(void) { if (isSingleton()) if (Instance != NULL) return Instance = new /* descendant constructor */; else return Instance; else return new /* descendant constructor */; }
出现的问题:如何在基类中声明类似工厂的方法? 我正在寻找C++类设计问题的解决方案。我试图实现的是在基类中使用静态方法,它将返回后代类型对象的实例。关键是,其中一些应该是单身。我在VCL中编写,所以有可能使用属性>代码>,但我更喜欢纯C++解决方案。 class Base { private: static Base *Instance; public: static Base *New(void); virtual bool isSingleton(void) = 0; } Base::Instance = NULL; class First : public Base { // singleton descendant public: bool isSingleton(void) { return true; } } class Second : public Base { // normal descendant public: bool isSingleton(void) { return false; } } Base *Base::New(void) { if (isSingleton()) if (Instance != NULL) return Instance = new /* descendant constructor */; else return Instance; else return new /* descendant constructor */; },c++,oop,C++,Oop,出现的问题: 如何声明静态变量实例,使其在子类中是静态的 如何在基类中调用后代构造函数 我想用我计划的方式来克服这些问题是不可能的。如果是这样的话,我想听听关于如何用其他方法解决这个问题的建议 编辑:代码中的一些小更改。我漏掉了几个指针标记。像这样的东西怎么样: class Base { public: virtual Base construct() = 0; }; class First : public Base { public: Base construct(){ retu
- 如何声明静态变量
,使其在子类中是静态的实例
- 如何在基类中调用后代构造函数
编辑:代码中的一些小更改。我漏掉了几个指针标记。像这样的东西怎么样:
class Base
{
public:
virtual Base construct() = 0;
};
class First : public Base
{
public:
Base construct(){ return First(); // or whatever constructor }
};
class Second : public Base
{
public:
Base construct(){ return Second(...); // constructor }
};
只是检查一下我们的术语是否同步——在我的书中,工厂类是一个类实例,它可以创建其他类的实例。创建哪种类型的实例取决于工厂接收到的输入,或者至少取决于工厂可以检查的内容。这里有一个非常简单的工厂:
class A { ~virtual A() {} };
class B : public A {};
class C : public A {};
class AFactory {
public:
A * Make( char c ) {
if ( c == 'B' ) {
return new B;
}
else if ( c == 'C' ) {
return new C;
}
else {
throw "bad type";
}
}
};
如果我是你,我会重新开始,记住这个例子和以下几点:
- factories不一定是单例
- 工厂不必是静态成员
- 工厂不必是它们创建的层次结构的基类成员
- 工厂方法通常返回动态创建的对象
- 工厂方法通常返回指针
- 工厂方法需要一种方法来决定创建哪个类的实例
<>我不明白为什么你的工厂需要反射,C++在任何情况下都不支持有意义的方式。 < P>根据“SeaDeDeP”的答案,我将使<代码> Base<代码>在实际类型上被模板化,使用:
您可以创建Singleton类和NonSingleton类,并使所有后代继承其中一个
class Base {
public:
static Base *New() = 0;
}
class SingletonDescendant: public Base {
public:
*Base::New() {
if (Instance != NULL)
return Instance = new /* descendant constructor */;
else
return Instance;
}
private:
static Base *Instance;
}
SingletonDescendant::Instance = NULL;
class NonSingletonDescendant: public Base {
public:
*Base::New() {
return new;
}
}
class First : public SingletonDescendant{ // singleton descendant
}
class Second : public NonSingletonDescendant{ // normal descendant
}
它解决了您提出的问题:
- 如何声明静态变量实例,使其在子类中是静态的:它只存在于SingletonDegenant类中
- 如何在基类中调用子代构造函数:使用新函数
- 我必须在每个后代中编写
方法;我认为它是多余的,因为很明显它必须做什么:现在它只在单个子后代和非单身后代中。construct()
实例==NULL
?
std::auto_ptr<First> first(First::construct());
std::auto_ptr<Second> second(Second::construct());
// do something with first and second...
class Base {
public:
static Base *New() = 0;
}
class SingletonDescendant: public Base {
public:
*Base::New() {
if (Instance != NULL)
return Instance = new /* descendant constructor */;
else
return Instance;
}
private:
static Base *Instance;
}
SingletonDescendant::Instance = NULL;
class NonSingletonDescendant: public Base {
public:
*Base::New() {
return new;
}
}
class First : public SingletonDescendant{ // singleton descendant
}
class Second : public NonSingletonDescendant{ // normal descendant
}