Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在基类中声明类似工厂的方法? 我正在寻找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 - Fatal编程技术网

如何在基类中声明类似工厂的方法? 我正在寻找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()
    方法;我认为它是多余的,因为很明显它必须做什么:现在它只在单个子后代和非单身后代中。

我现在正在使用这个方法,但是我必须在每个子代中写“代码>构造())/Cult>方法——我认为它是多余的,因为它显然是要做的。我认为通常的建议是调用你的“工厂”方法<代码> CREATE()>代码>,因为它们非常具体地不是构造函数。和您提供的一样,每当我添加新的子类时,我都必须为Make()方法添加代码。我认为这是多余的和有害的,因为我想把我的基类作为一个封闭的框架。您可以在工厂注册新类,而无需更改工厂代码。这是一个非常常见的模式——你读过一本关于这个主题的好书吗?萨穆尔:考虑使用一个抽象工厂,在运行时你可以登记具体的工厂。您只需编写一次抽象工厂代码(它允许注册具体工厂),然后为希望抽象工厂创建的每个新类型创建一个新的具体工厂。这是一种很常见的模式。@Neil Butterworth:我读过Ruby中的模式。C++是完全不同的,需要更多的模式。感谢您的建议,解决方案开始出现;)有趣的方法。我目前正在尝试看看它是否适合我的需要,并替换Neil Butterworth提到的典型工厂模式。单例和非单例后代之间的区别看起来不错,但您留下了我的注释/*后代构造函数*/。因此,我必须为每个子代实现新方法。你是说
实例==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
}