C++:原始指针的容器

发布于 2024-09-24 17:13:32 字数 798 浏览 0 评论 0原文

我需要在 C++ 中存储对派生类实例的引用。我考虑过对基类使用shared_ptrs向量(因为它需要保存不同类型的派生类),但是,容器保存原始指针很重要,而向量(或其他stl容器)则不是这种情况,如果我没记错的话。有没有办法在本机 C++ 中执行此操作,或者我是否必须使用特殊容器(例如 Boost 的 ptr_vector)?

编辑:这是我的测试代码:

class Foo
{
public:
    Foo() {}
    virtual ~Foo() {}
    virtual void set_x(int i) = 0;
};

class Bar : public Foo
{
public:
    Bar() {}

    void set_x(int i)
    {
        this->x = i;
    }

    int x;
};

int main()
{
    Bar bar;

    // ptr
    std::cout << &bar << "\n";

    std::vector<Foo*> foos;
    foos.push_back(&bar);

    // different ptr value
    std::cout << &foos[0] << "\n";

    foos[0]->set_x(1);

    // however, changes are made
    std::cout << bar.x;

    return 0;
}

提前致谢,

jena

I need to store references to instances of derived classes in C++. I considered using a vector of shared_ptrs to the base class (for it needs to hold different types of derived classes), however, it's important that the container holds the original pointers, which is not the case with vectors (or other stl containers), if I'm not mistaken. Is there a way to do this in native C++, or do I have to use special containers like Boost's ptr_vector?

EDIT: This is my test code:

class Foo
{
public:
    Foo() {}
    virtual ~Foo() {}
    virtual void set_x(int i) = 0;
};

class Bar : public Foo
{
public:
    Bar() {}

    void set_x(int i)
    {
        this->x = i;
    }

    int x;
};

int main()
{
    Bar bar;

    // ptr
    std::cout << &bar << "\n";

    std::vector<Foo*> foos;
    foos.push_back(&bar);

    // different ptr value
    std::cout << &foos[0] << "\n";

    foos[0]->set_x(1);

    // however, changes are made
    std::cout << bar.x;

    return 0;
}

Thanks in advance,

jena

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

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

发布评论

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

评论(3

南薇 2024-10-01 17:13:32

在上面的示例中,您打印出的是指针的地址而不是指针的值。

而不是:除此之外

// different ptr value
std::cout << &foos[0] << "\n";

// different ptr value
std::cout << foos[0] << "\n";

您的 vector 将工作得很好。

In your example above, what you are printing out is the address of the pointer not the value of the pointer.

Instead of:

// different ptr value
std::cout << &foos[0] << "\n";

Do

// different ptr value
std::cout << foos[0] << "\n";

Aside from that your vector<Foo*> will work just fine.

记忆之渊 2024-10-01 17:13:32

您可以创建一个 std::vector​​ ,它将保存您传递给它的任何指向 foo 的指针。它不会尝试在销毁时删除这些指针,这可能是也可能不是您想要的,但它将准确保存您传入的值。

您还可以创建一个 std::向量< Shared_ptr; >,它将保存指针,一旦没有共享指针的悬空副本浮动,这些指针就会被释放。这些还将保存您传入的“原始”foo*;您可以使用shared_ptr::get()方法再次获取它。

唯一看不到与派生对象完全相同的指针的情况是,如果您使用类的多重继承,并且基类包含数据。因为在这种情况下, foo* 最终会指向数据的“foo”部分,而该部分不一定位于对象的“根”处。

You can create a std::vector<foo*>, which will hold any pointers to foo that you hand to it. It won't make any attempt to delete those pointers on destruction, which may or may not be what you want, but it will hold exactly the values you pass in.

You can also create an std::vector< shared_ptr<foo> >, which will hold pointers that will be released once there are no dangling copies of the shared_ptr floating around. Those will also hold the "original" foo* you passed in; you can get it again by using the shared_ptr::get() method.

The only time you wouldn't see exactly the same pointer as your derived object is if you're using multiple inheritance of classes, and your base classes include data. Because a foo* would end up, in that case, pointing to the "foo" part of the data, which wouldn't necessarily be at the "root" of the object.

数理化全能战士 2024-10-01 17:13:32

如果您使用shared_ptr作为容器成员,则每个成员中的指针将保留对原始对象实例的访问权限。在容器管理之后,您可以随时获取 shared_ptr 的副本,但原始对象仍然是其目标。

对于更简单的解决方案,您可以使用 boost::ptr_vector,前提是您的指针不会在容器中出现两次 - 这种情况会引入棘手的资源管理复杂性,并将您引回到 shared_ptr >。

If you use shared_ptr as your container member, the pointer in each member will retain access to the original object instance. You can get a copy of a shared_ptr at any point after container housekeeping, but the original object will still be its target.

For a simpler solution you might use boost::ptr_vector, provided none of your pointers occur twice in the container - that scenario would introduce tricky resource management complexity and point you back to shared_ptr.

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