使用 CRTP 时如何访问基类构造函数

Hon*_*nza 4 c++ crtp

我需要在我的类层次结构中插入克隆并创建成员函数

class Base
{
protected:
    const int x_;
public:
    Base() : x_(0) {}
    Base(int x) : x_(x) {}
};
Run Code Online (Sandbox Code Playgroud)

我认为 CRTP 可能是节省一些打字并避免错误的方法。

template <typename Derived>
class CRTP_Iface : public Base
{
public:
    virtual Base *create() const { return new Derived(); }
    virtual Base *clone() const { return new Derived(static_cast<Derived const&>(*this)); }
};
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法访问基类构造函数来初始化 const 成员。

class D1 : public CRTP_Iface<D1>
{
public:
    D1() : Base() {}
    D1(int x) : Base(x) {}
};

class D2 : public CRTP_Iface<D2>
{
public:
    D2() : x_(0) {}
    D2(int x) : x_(x) {}
};

int main()
{
    D1 a;
    D2 b;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有什么简单的方法可以解决这个问题吗?

Kor*_*icz 5

只需将所有需要的构造函数添加到CRTP_Iface.

public:
  CRTP_Iface() : Base() {}
  CRTP_Iface( int x ) : Base(x) {}
Run Code Online (Sandbox Code Playgroud)

如果使用 C++11,这会变得更容易:

public:
  using Base::Base;
Run Code Online (Sandbox Code Playgroud)

那么你有:

class D1 : public CRTP_Iface<D1>
{
public:
    D1() : CRTP_Iface() {}
    D1(int x) : CRTP_Iface(x) {}
};
Run Code Online (Sandbox Code Playgroud)

...用 C++11 写得更好:

class D1 : public CRTP_Iface<D1>
{
public:
  using CRTP_Iface<D1>::CRTP_Iface;
};
Run Code Online (Sandbox Code Playgroud)

(不确定 :: 的左手还是右手是否需要,AFAIR 一些编译器喜欢它更严格)