如何使用“Get”分配成员值功能?

发布于 2025-01-10 11:56:40 字数 533 浏览 4 评论 0原文

这是一个简单的代码。

class Sub
{
    ...
public:
    Sub()
    {
        ...
    }
}

class Main
{
private:
    Sub*    m_pSub

public:

    Main()
    {
        // I don't want construct "Sub" here
        m_pSub = nullptr;
    }    

    Sub*    GetSub()
    {
        return m_pSub;
    }
}


/////////////////////
// in source
Main*   pMain;

pMain->GetSub() = new Sub()

当然,pMain->GetSub() = new Sub()不起作用,因为上面代码中'='的左边值必须是可纠正的值。

因此,请教我各种类似的实施方法 (可以使用尽可能短的)。

谢谢 !

Here's a simple code.

class Sub
{
    ...
public:
    Sub()
    {
        ...
    }
}

class Main
{
private:
    Sub*    m_pSub

public:

    Main()
    {
        // I don't want construct "Sub" here
        m_pSub = nullptr;
    }    

    Sub*    GetSub()
    {
        return m_pSub;
    }
}


/////////////////////
// in source
Main*   pMain;

pMain->GetSub() = new Sub()

Of course, pMain->GetSub() = new Sub() does not work because the left value of '=' in the above code must be a correctable value.

Therefore, please teach me various ways to implement similarly
(which can be used as short as possible).

Thank you !

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

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

发布评论

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

评论(1

一枫情书 2025-01-17 11:56:40

让代码工作的最简单方法是让 GetSub() 返回一个引用,例如:

class Main
{
private:
    Sub* m_pSub = nullptr;

public:

    Main() = default;

    Sub*& GetSub()
    {
        return m_pSub;
    }
};

但是,这不是很好的类设计。

另一种选择是让 GetSub() 在第一次调用时创建对象,例如:

class Main
{
private:
    Sub* m_pSub = nullptr;

public:

    Main() = default; 

    Sub* GetSub()
    {
        if (!m_pSub) m_pSub = new Sub;
        return m_pSub;
    }
};

否则,使用显式 setter 方法,例如:

class Main
{
private:
    Sub* m_pSub = nullptr;

public:

    Main() = default;    

    Sub* GetSub()
    {
        return m_pSub;
    }

    void SetSub(Sub* newSub)
    {
        m_pSub = new Sub;
    }
};

...
pMain->SetSub(new Sub);

无论哪种方式,您确实应该使用智能指针,或者 std::unique_ptrstd::shared_ptr,以明确谁拥有 Sub 对象并负责销毁它。原始指针不传达该信息。

The simplest way to make your code work is to have GetSub() return a reference, eg:

class Main
{
private:
    Sub* m_pSub = nullptr;

public:

    Main() = default;

    Sub*& GetSub()
    {
        return m_pSub;
    }
};

However, this isn't very good class design.

Another option is to have GetSub() create the object on its first call, eg:

class Main
{
private:
    Sub* m_pSub = nullptr;

public:

    Main() = default; 

    Sub* GetSub()
    {
        if (!m_pSub) m_pSub = new Sub;
        return m_pSub;
    }
};

Otherwise, use an explicit setter method, eg:

class Main
{
private:
    Sub* m_pSub = nullptr;

public:

    Main() = default;    

    Sub* GetSub()
    {
        return m_pSub;
    }

    void SetSub(Sub* newSub)
    {
        m_pSub = new Sub;
    }
};

...
pMain->SetSub(new Sub);

Either way, you really should be using smart pointers, either std::unique_ptr or std::shared_ptr, to make it clear who owns the Sub object and is responsible for destroying it. A raw pointer does not convey that information.

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