简体   繁体   English

有什么方法可以维护对 object 的引用的结构数组? (C++)

[英]Any way to maintain a struct array of references to an object? (C++)

I have a matrix that I want to be able to share access to via a struct array containing references to said matrix.我有一个矩阵,我希望能够通过包含对所述矩阵的引用的结构数组来共享访问。

Is there any way to achieve this so I can modify the matrix by pulling from this buffer?有没有办法实现这一点,所以我可以通过从这个缓冲区中拉出来修改矩阵?

struct something {
    vector<vector<double>> &a;
};

something buffer[3];

void modify(something &arr) {
    (arr.a)[0][0] = 10;
    cout << (arr.a)[0][0] << endl;
}

int main(int argc, const char * argv[]) {
    vector<vector<double>> a(5, vector<double>(5));
    buffer[0] = {a};
    modify(buffer[0]);
    // want this to print 10
    cout << a[0][0] << endl;
    return 0; 
}

The problem with this, of course, is that a struct with a reference inside of it does not have a default constructor and thus can't be initialized in this way.当然,这样做的问题是内部带有引用的结构没有默认构造函数,因此无法以这种方式初始化。

You need to create the matrices first so that you can refer to them.您需要先创建矩阵,以便您可以参考它们。 That way you can initialise the references correctly:这样您就可以正确初始化引用:

vector<vector<double>> matrices[3];
something buffer[]{matrices[0], matrices[1], matrices[2]};

That is not the only problem however.然而,这不是唯一的问题。 Another is that you attempt to assign something :另一个是你试图分配something

 buffer[0] = {a};

This is not allowed because something is not assignable.这是不允许的,因为something是不可分配的。 It is not assignable because the class contains a reference member.它不可分配,因为 class 包含引用成员。

 // want this to print 10

There is no way to make the global reference refer to a local variable.没有办法让全局引用引用局部变量。

Given that you are trying to use the class in several ways that are contradictory to having a reference member, perhaps it would be better to re-design it to not use a reference.鉴于您正在尝试以与拥有参考成员相矛盾的几种方式使用 class,也许最好重新设计它以不使用参考。

This seems like a potentially good use case for a pointer.这似乎是指针的潜在好用例。 Unlike references, pointers can be assigned after initialisation.与引用不同,指针可以在初始化后分配。 Be very careful however when storing pointers to local objects in the global array.但是,在全局数组中存储指向本地对象的指针时要非常小心。 It is very easy to end up with an invalid pointer.很容易得到一个无效的指针。

It's best to think of a reference as not an actual object, but an abstraction for "a way to access the object".最好将引用视为不是实际的 object,而是“访问对象的方法”的抽象。 Since it's not concrete data, it cannot be put into containers.由于它不是具体数据,因此不能放入容器中。 Your best bet for achieving a similar effect is to create an array of pointers.实现类似效果的最佳选择是创建一个指针数组。 Depending on your C++ standard and memory allocation needs you will need to choose between raw or C++11's smart pointers (from the <memory> header).根据您的 C++ 标准和 memory 分配需求,您将需要在原始或 C++11 的智能指针(来自<memory>标头)之间进行选择。

This is a good use case for pointers, as they can be default constructed and you can assign different variables to them, unlike references:这是指针的一个很好的用例,因为它们可以是默认构造的,并且您可以为它们分配不同的变量,这与引用不同:

struct something {
    vector<vector<double>> *a = nullptr; //pointer variable
};

something buffer[3];

void modify(something &arr) {
    //include nullptr check to prevent segmentation error
    if(!arr.a) return;
    //dereference arr.a to access it
    (*(arr.a))[0][0] = 10; 
    cout << (*(arr.a))[0][0] << endl; //dereference arr.a to access it
}

int main(int argc, const char * argv[]) {
    vector<vector<double>> a(5, vector<double>(5));
    buffer[0] = {&a};
    modify(buffer[0]);
    // want this to print 10
    cout << a[0][0] << endl;
    return 0; 
}

Be careful to track the lifetime of the objects pointed to by the pointers, as if they go out of scope before the pointers do, the pointers will become dangling.小心跟踪指针指向的对象的生命周期,就好像它们在指针做之前 go 超出 scope 一样,指针将变得悬空。

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

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