gpt4 book ai didi

c++ - 是否可以在 C++ 中声明基类,以便只能通过创建函数创建从它派生的类?

转载 作者:搜寻专家 更新时间:2023-10-31 02:07:40 26 4
gpt4 key购买 nike

类似下面的内容

class Base
{
protected:
Base()
{}

public:
virtual void initialize()
{}

template<typename D, typename... Ts>
static std::shared_ptr<D> create(Ts... args)
{
auto d = std::shared_ptr<D>(new D(args...));
d->initialize();
return d;
}
};

class Derived : public Base
{
private:
int value_;

protected:

public:

Derived(int v) : value_(v)
{}

void initialize() override
{
//Do something with value_
}
};


int main()
{
auto derived = Base::create<Derived>(42);
// etc.

return 0;
}

上面的代码按预期工作,但如果 Base 是库的用户通过实现 Derived 扩展的类库的一部分,则没有什么可以阻止库的用户通过其公共(public)构造函数创建 Derived 并因此初始化 won '被称为。

在这样的库/库用户场景中,如何强制使用诸如 Base::create(...) 之类的东西来创建派生类?

例如,如果 Base::Base() 设为私有(private)而不是 protected ,则代码将无法编译,因为 Derived::Derived() 将无法访问它。

最佳答案

这个解决方案有点挑剔,但我认为它涵盖了所有基础:

class Base
{
protected:

// Token is only visible to Derived, and only constructible by Base
class token {
friend Base;
token() {};
};

// Constructing Base requires a token
Base(token)
{}

public:
// bla

template<typename D, typename... Ts>
static std::shared_ptr<D> create(Ts... args)
{
auto d = std::shared_ptr<D>(new D({}, args...));
// Token is created here ^^

// bla
}
};

class Derived : public Base
{
// bla

public:

// Derived receives and forwards the token to Base
// The implicit copy constructor is accessible
Derived(token t, int v) : Base(t), value_(v)
{}

// bla
};

因此,没有人可以编写 Derived d(Base::token{}, 42) 因为 token 是不可访问的。 Derived 本身可以命名 token 并传递一个,但不能创建一个:只有 Basecreate 函数可以.

关于c++ - 是否可以在 C++ 中声明基类,以便只能通过创建函数创建从它派生的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com