简体   繁体   English

创建包含引用的对象数组

[英]Creating an array of objects containing references

What is the best way to deal with the following situation? 处理以下情况的最佳方法是什么?

Suppose that I have something that should behave like this 假设我的行为应该像这样

class Foo
    {
    public:
         Foo(Bar& bar):m_bar(bar){}
    private:
         Bar& m_bar;
    };

Foo:s must have a valid reference to Bar:s. Foo:s必须有效引用Bar:s。 Also different Foo:s needs different or the same Bar:s. 同样,不同的Foo:s需要不同或相同的Bar:s。

I want to store Foo:s in an array. 我想将Foo:s存储在数组中。 However, since Foo will require a non-default constructor, it will not work. 但是,由于Foo将需要非默认构造函数,因此它将不起作用。

I could create an array of pointers to Foo:s, but then I need to call new and delete for each object in that array. 我可以创建一个指向Foo:s的指针数组,但是随后我需要为该数组中的每个对象调用new和delete。

I could define Foo like this instead 我可以这样定义Foo

class Foo
    {
    public:
         void init(Bar& bar)
             {
             m_bar=&bar;
             }
    private:
         Bar* m_bar;
    };

, but then it is possible to create uninitialized Foo:s. ,但是可以创建未初始化的Foo:s。

What about some sort of placement new? 那么某种新的安置方式呢?

You could use pointers still but instead of using raw pointers use unique_ptrs. 您可以仍然使用指针,但是可以使用unique_ptrs代替原始指针。 These are available in c++11, but if you are using an older compiler you could use the boost implementation. 这些在c ++ 11中可用,但是如果您使用的是较旧的编译器,则可以使用boost实现。

For example 例如

class Foo
{
public:
  Foo(unique_ptr<Bar> bar):m_pBar(bar){}
private:
  unique_ptr<Bar> m_pBar;
};

This way you don't have to worry about calling delete on your Bar objects as they are deleted once there are no more references to them. 这样,您就不必担心在Bar对象上调用delete,因为一旦不再有对它们的引用,它们就会被删除。

Then you can use Foo like this 然后你可以像这样使用Foo

unique_ptr<Bar> pBar(new Bar());
Foo(pBar);

EDIT: Changed to use unique_ptr instead of shared_ptr and added example usage 编辑:更改为使用unique_ptr而不是shared_ptr,并添加了示例用法

First, sorry for my first answer I got the question wrong. 首先,对不起我的第一个回答,我把问题弄错了。 Here is a better solution I hope: you can create a vector of elements without default constructor like this: 我希望这是一个更好的解决方案:您可以在没有默认构造函数的情况下创建元素vector ,如下所示:

#include <iostream>
#include <vector>

using namespace std;


class Foo 
{ 
public: 
    Foo(int& bar): m_pBar(bar)
    { } 

    Foo& operator=(const Foo& other)
    { m_pBar = other.m_pBar;  }

    int Get()
    { return m_pBar; }

private: 
    int& m_pBar; 
}; 


int main(int argc, char** argv)
{
    int test[10]  = { 0 };

    vector<Foo> vect;
    for (int& i: test)
        vect.emplace_back(i);

    test[0] = 1;
    cout << vect[0].Get() << endl;

    return 0;
}

Finally I implemented a custom container with the following constructor: 最后,我使用以下构造函数实现了一个自定义容器:

template<class T>
template<class U,class V>
Array<T>::Array(unsigned int n,U source_iterator,const V& source_resource):memory(n*sizeof(T))
    {
    data=(T*)memory.pointerGet();
    unsigned int k;
    try
        {
        for(k=0;k<n;k++)
            {
            new(data+k)T(*source_iterator,source_resource);
            ++source_iterator;
            }
        }
    catch(...)
        {
        while(k>0)
             {
             k--;
             data[k].~T();
             }
        throw;
        }
    length=n;
    capacity=n;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM