是否可以编写C++基类成员函数来实例化从中调用它的任何派生类?

Can a C++ base class member function be written to instantiate whatever derived class it's called from?

本文关键字:调用 任何 派生 实例化 C++ 函数 成员 基类 是否      更新时间:2023-10-16

有没有任何方法可以在基类中实现一次函数,该函数将返回从中调用的任何派生类的实例?

template <class TDerived> class Base
{
    TDerived retInstance()
    {
        return TDerived();
    }
};
class Derived : Base<Derived>
{
    //class definition here
};

如果您选择的语言支持协变返回类型,您可以执行类似于以下C++代码示例的操作:

struct A {
    virtual A *make() = 0;
};
struct B : public A {
    B *make() override {
        return new B{};  
    }
};

虽然这不符合在基类中定义一次的标准,但我认为值得一提。

如前所述,

  1. CRTP(奇怪的循环模板模式)

  2. 或可克隆模式。


1.CRTP

在Coliru上查看Liv

template <typename Derived>
struct BaseImpl
{
    // normal stuff
    int foo() const { return 42; }
    // CRTP stuff
    Derived make_new() const 
    {
        return Derived("test 123");
    }
};
#include <string>
struct MyStruct : BaseImpl<MyStruct>
{
    std::string value;
    MyStruct(std::string const& value) : value(value) {}
};
#include <iostream>
int main()
{
    MyStruct a("first");
    MyStruct b = a.make_new();
    std::cout << a.value << "n"
              << b.value << "n";
}

打印

first
test 123

2.可克隆模式:

看到它在Coliru上直播

struct ICloneable
{
    virtual const char* whoami() const = 0;
    virtual ICloneable* clone() const = 0;
    virtual ~ICloneable() throw() {}
};
#include <string>
struct A : ICloneable
{
    virtual const char* whoami() const { return "struct A"; }
    virtual ICloneable* clone() const { return new A; }
};
struct B : ICloneable
{
    virtual const char* whoami() const { return "struct B"; }
    virtual ICloneable* clone() const { return new B; }
};
#include <iostream>
#include <typeinfo>
int main()
{
    A a;
    B b;
    ICloneable* aclone = a.clone();
    ICloneable* bclone = b.clone();
    std::cout << typeid(*aclone).name() << "n";
    std::cout << typeid(*bclone).name() << "n";
    delete aclone;
    delete bclone;
}

打印(取决于编译器):

1A
1B
相关文章: