非拥有指针作为现代 C++ 中的类成员 [英] Non-owning pointer as class member in modern c++

查看:29
本文介绍了非拥有指针作为现代 C++ 中的类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究智能指针以及何时应该使用它们与原始指针.我认为拥有指针的对象应该将该指针实例化为智能指针(unique_ptr/shared_ptr).

I have been researching smart pointers and when they should be used vs raw pointers. My take away was that objects which OWN the pointer should instantiate that pointer as a smart pointer (unique_ptr/shared_ptr).

然后,当将该指针传递给不需要其所有权的各种函数/类时,传递一个原始指针(通过 .get() 从智能指针获得),但需要注意的是原始指针被发送到的函数/类永远不会超过拥有它的函数/类的范围.

Then, when passing that pointer to various functions / classes that do not require ownership of it, pass a raw pointer (obtained from the smart pointer via .get()) with the caveat that the function / class that the raw pointer is sent to will NEVER outlive the scope of the function / class which owns it.

这种理解正确吗?这对我来说似乎很有意义,但是当我开始实施特定的例程时,我的信心下降了.

Is this understanding correct? It seems to make sense to me, but when I started to implement a specific routine my confidence dwindled.

以下代码将更好地说明我当前挂断的位置.我在 Scene 中有一个 unique_ptrMesh 对象的成员向量.然后我传递创建的 unique_ptr 的原始指针来实例化一个 Actor 对象,最后将 unique_ptr 移动到 scene->;网格 向量.

The following code will better explain where my hangup is currently. I have a member vector of unique_ptrs to Mesh objects in Scene. I then pass the raw pointer of the created unique_ptr to instantiate an Actor object and finally move the unique_ptr into the scene->meshes vector.

Actor 有一个 Mesh* 作为成员的事实,只要任何一个 Actor 的作用域就不会被认为是不好的做法吗?永远不会超过拥有 scene->meshes 元素的那个元素?

Is the fact that an Actor has a Mesh* as a member not considered bad practice as long as the scope of any one Actor will never outlive that of the owning scene->meshes element?

#include <iostream>
#include <vector>
#include <memory>

class Mesh
{
public:
    int vec = 1;
    int ind = 2;
};

class Actor
{
private:
    Mesh * mesh;
public:
    Actor(Mesh* m) {
        this->mesh = m;
    }
};

class Scene
{
public:
    // meshes will always outlive all instantiated actors
    std::vector<std::unique_ptr<Mesh>> meshes;
    std::vector<Actor> actors;
};


int main()
{
    auto scene = std::make_unique<Scene>();

    auto m1 = std::make_unique<Mesh>();
    auto a1 = Actor(m1.get());
    scene->meshes.push_back(std::move(m1));

    auto m2 = std::make_unique<Mesh>();
    auto a2 = Actor(m2.get());
    scene->meshes.push_back(std::move(m2));

    std::cout << scene->meshes.size();

    std::cin.get();
    return 0;
}

推荐答案

Actor 拥有 Mesh* 作为成员的事实是否被认为是不好的做法,只要...

Is the fact that an Actor has a Mesh* as a member not considered bad practice as long as ...

当然,这是一种代码异味.该类很容易被滥用.必须仔细记录构造函数在 actor 的生命周期中存储指针,以提醒用户维护指向网格的生命周期.

It is a code smell, sure. The class is easy to misuse. It has to be carefully documented that the constructor stores the pointer for the lifetime of the actor to alert the user to maintain the lifetime of the pointed mesh.

在这种情况下,共享所有权会更安全.但如果性能开销很大,它并不总是最佳选择.

In case such as this, shared ownership would be safer. But it is not always the best choice if the performance overhead is significant.

这篇关于非拥有指针作为现代 C++ 中的类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆