C++ 在C+中从基调用潜在子级的构造函数+;

C++ 在C+中从基调用潜在子级的构造函数+;,c++,oop,inheritance,overriding,C++,Oop,Inheritance,Overriding,如何实现此模式: class Base {//doesn't know anything about potential descendant-classes, like Child public: Base * foo( void) { //some code return ( Base *) new !Child-constructor!(); } }; class Child : public Base { };

如何实现此模式:

  class Base {//doesn't know anything about potential descendant-classes, like Child
  public:
      Base * foo( void) {
        //some code
          return ( Base *) new !Child-constructor!();
      }
  };

  class Child : public Base { };

//—————————————————————————————————————————————————
  #include <iostream>
  #include <typeinfo>

  using namespace std;

  int main( void) {
      Base * base_p = Child().f();

      cout << typeid( *base_p).name(); //expected to get "Child"

  return 0;
  }
类基类{//对潜在的子类一无所知,比如Child
公众:
基本*foo(无效){
//一些代码
返回(基*)新的!子构造函数!();
}
};
类子类:公共基{};
//—————————————————————————————————————————————————
#包括
#包括
使用名称空间std;
内部主(空){
Base*Base_p=Child().f();

顺便说一句,这个设计很糟糕

 //terriblecode.hpp

 struct Base
 {
    Base * foo(void);
 };

 struct Child : public Base{};

 //terriblecode.cpp
 Base* Base::foo() {return (Base*) new Child();}

在定义foo之前,不需要定义child。将您的声明与成员函数的定义分开,这样做非常容易。

只需确保在首次使用之前让编译器知道您的派生类:

class Child; // forward declaration

class Base {
public:
  Base * foo( void);
};

class Child : public Base { };

// the function needs to be defined after the "Child" is known
Base * Base::foo( void) {
  //some code
    return new Child; // will be automatically type-cast to base class
}

//—————————————————————————————————————————————————
#include <iostream>
#include <typeinfo>

using namespace std;

int main( void) {
  Base * base_p = Child().f();

  cout << typeid( *base_p).name(); //expected to get "Child"

  return 0;
}
class子类;//转发声明
阶级基础{
公众:
基本*foo(无效);
};
类子类:公共基{};
//函数需要在“子”已知后定义
Base*Base::foo(void){
//一些代码
return new Child;//将自动类型转换为基类
}
//—————————————————————————————————————————————————
#包括
#包括
使用名称空间std;
内部主(空){
Base*Base_p=Child().f();
库特
“…在基类的定义中不能知道该子类。因此,我希望foo调用将在其中继承该子类的类的构造函数。”

IMHO最简单的方法是提供一个带有
Base

class Base {
public:
    template<class Derived>
    static std::unique_ptr<Base> foo( void) {
      //some code
      return std::unique_ptr<Base>(new Derived());
    }
};

class Child : public Base {
public:
    Child() {}
    virtual ~Child() {}
};

int main() {
    std::unique_ptr<Base> p = Base::foo<Child>();
    return 0;
}
类基{
公众:
模板
静态标准::唯一的\u ptr foo(无效){
//一些代码
返回std::unique_ptr(新派生的());
}
};
类子:公共基{
公众:
子(){}
虚拟~Child(){}
};
int main(){
std::unique_ptr p=Base::foo();
返回0;
}

检查可编译的示例。

忘了说,我不能使用C++11。
foo
的定义需要
Child
的定义。
Child
的定义需要
Base
的定义,它只需要声明
foo
。这里没有问题。那么我应该写什么来代替
!C>hild构造函数!()
Child
,就像你在
main
中使用的一样。你是否
clone()
?当然:)让它出格。让我更改它RQT转发声明没有任何作用。@YePhIcK,谢谢你的回答,但我忘了提到(我认为不会有误解),在
Base
的定义中不能知道
Child
类。所以我想
foo
调用继承它的类的构造函数。放弃C样式转换,向上转换是隐式的。谢谢,我想我会使用它。
class Base {
public:
    template<class Derived>
    static std::unique_ptr<Base> foo( void) {
      //some code
      return std::unique_ptr<Base>(new Derived());
    }
};

class Child : public Base {
public:
    Child() {}
    virtual ~Child() {}
};

int main() {
    std::unique_ptr<Base> p = Base::foo<Child>();
    return 0;
}