创建一个 const share_ptr;成员

发布于 2024-12-13 15:22:35 字数 708 浏览 2 评论 0原文

我有许多从纯虚拟基派生的类:

class base {
public:
    virtual int f() = 0;
};

class derived_0 : public base {
public:
    int f() {return 0;}
};

class derived_1 : public base {
public:
    int f() {return 1;}
};

为了简洁起见,我只放置了两个派生类,但实际上我有更多类。

我想创建一个具有指向基类的 const 共享指针的类。我想执行以下操作,但不能,因为我必须初始化初始化列表中的 const 指针:

class C{
public:
    C(bool type) { 
        if(type) {
            derived_0* xx = new derived_0;
            x = shared_ptr<base>( xx );
        }
        else {
            derived_1* xx = new derived1;
            x = shared_ptr<base>( xx );
        }
    } 
private:
    const share_ptr<base> x;
};

如何获得此功能?

I have a number of classes that derive from a pure virtal base:

class base {
public:
    virtual int f() = 0;
};

class derived_0 : public base {
public:
    int f() {return 0;}
};

class derived_1 : public base {
public:
    int f() {return 1;}
};

I only put two derived classes for brevity, but in practice I have more.

And I would like to create a class that has a const shared pointer to the base. I would like do to the following but I can't as I must initialize the const pointer in the initialization list:

class C{
public:
    C(bool type) { 
        if(type) {
            derived_0* xx = new derived_0;
            x = shared_ptr<base>( xx );
        }
        else {
            derived_1* xx = new derived1;
            x = shared_ptr<base>( xx );
        }
    } 
private:
    const share_ptr<base> x;
};

How can I get this functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-12-20 15:22:35

您将对象的创建封装在函数中,如下所示:

shared_ptr<base> create_base(bool type) {
     if(type) {
         return make_shared<derived_0>();
     }
     else {
         return make_shared<derived_1>();
     }
}

然后您可以在初始化列表中使用它:

class C{
public:
    C(bool type)
    : x(create_base(type))
    {}
private:
    const share_ptr<base> x;
};

You encapsulate the creation of the object in a function, like this:

shared_ptr<base> create_base(bool type) {
     if(type) {
         return make_shared<derived_0>();
     }
     else {
         return make_shared<derived_1>();
     }
}

And then you can use it in your initialization-list:

class C{
public:
    C(bool type)
    : x(create_base(type))
    {}
private:
    const share_ptr<base> x;
};
尴尬癌患者 2024-12-20 15:22:35

在像这个精确示例这样的简单情况下:(

class C
{
    shared_ptr<Base> const x;
public:
    C( bool type ) 
        : x( type
            ? static_cast<Base*>( new Derived_0 )
            : static_cast<Base*>( new Derived_1 ) )
    {
    }
};

是的,static_cast 或至少其中之一是必要的。)

在更一般的情况下,决策逻辑更复杂,您可以
可能想要创建一个返回 shared_ptr 的静态函数,
例如:

class C
{
    shared_ptr<Base> const x;
    static shared_ptr<Base> makeSharedPtr( bool type );
public:
    C( bool type )
        : x( makeSharedPtr( type ) )
    {
    }
};

这将允许任何可以想象的逻辑(以及一组更复杂的逻辑)
参数也是如此)。

In simple cases like this precise example:

class C
{
    shared_ptr<Base> const x;
public:
    C( bool type ) 
        : x( type
            ? static_cast<Base*>( new Derived_0 )
            : static_cast<Base*>( new Derived_1 ) )
    {
    }
};

(And yes, the static_cast, or at least one of them, are necessary.)

In more general cases, where the decision logic is more complex, you
might want to create a static function which returns the shared_ptr,
e.g.:

class C
{
    shared_ptr<Base> const x;
    static shared_ptr<Base> makeSharedPtr( bool type );
public:
    C( bool type )
        : x( makeSharedPtr( type ) )
    {
    }
};

This will allow any imaginable logic (and a more complex set of
parameters as well).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文