c++:创建一个带有引用变量的新对象。

时间:2021-07-06 23:34:17

In a function I get as an argument the reference to a vector:

在函数中,我得到了一个关于向量的参数:

void myFunc (vector< int>& myRef) {...}

Now in this function I need to backup (because myRef will become something else) the reference to that particular vector:

现在,在这个函数中,我需要备份(因为myRef将会变成别的东西)指向那个特定的向量:

vector< int>& myBackup = myRef

Now I want myRef to become a reference to a NEW COPY of that vector. How do I do that?

现在,我希望myRef成为对该向量的新副本的引用。我该怎么做呢?

Can I just say

我只是能说

vector< int> myRef = myBackup

?

吗?

Is the result myBackup = a reference to the original object and myRef = a reference to a copy of the original object?

结果myBackup =对原始对象的引用和myRef =对原始对象副本的引用吗?

3 个解决方案

#1


2  

C++ references cannot be changed to refer to new objects after they are initially assigned. If you need that kind of behavior, you have to use a pointer.

不能更改c++引用,以在它们最初被分配后引用新对象。如果你需要这种行为,你必须使用一个指针。

#2


0  

Does this code compile? You should really make use of a pointer here, if you want to reassign, as references can't be reassigned. A simple rule of thumb is if you want to be able to reassign the variable use a pointer, so long as it is advisable to do so in the current situation.

这段代码编译吗?如果你想重新分配,你应该在这里使用一个指针,因为引用不能被重新分配。一个简单的经验法则是,如果您希望能够重新分配变量使用一个指针,只要在当前的情况下这样做是明智的。

For example:

例如:

void myFunction(std::vector<int*> myPtr) {}

#3


0  

To solve this problem you need to make deep copy You can use memcpy but it is not a safe way; or such as

为了解决这个问题,你需要做一个深入的复制,你可以使用memcpy,但它不是一个安全的方法;或者如

vector<int> newVector;
newVector.reserve(myRef.size()); //for performance, to avoid realloc
for (vector< int>::iterator it = myRef.begin();it!=myRef.end();++it)
   newVector.push_back(*it);
myRef = newVector;

And after this line

后,这条线

vector< int>& myBackup = myRef

you have a compiler error with redeclaration of myRef...

您有一个编译错误,重新声明myRef…

#1


2  

C++ references cannot be changed to refer to new objects after they are initially assigned. If you need that kind of behavior, you have to use a pointer.

不能更改c++引用,以在它们最初被分配后引用新对象。如果你需要这种行为,你必须使用一个指针。

#2


0  

Does this code compile? You should really make use of a pointer here, if you want to reassign, as references can't be reassigned. A simple rule of thumb is if you want to be able to reassign the variable use a pointer, so long as it is advisable to do so in the current situation.

这段代码编译吗?如果你想重新分配,你应该在这里使用一个指针,因为引用不能被重新分配。一个简单的经验法则是,如果您希望能够重新分配变量使用一个指针,只要在当前的情况下这样做是明智的。

For example:

例如:

void myFunction(std::vector<int*> myPtr) {}

#3


0  

To solve this problem you need to make deep copy You can use memcpy but it is not a safe way; or such as

为了解决这个问题,你需要做一个深入的复制,你可以使用memcpy,但它不是一个安全的方法;或者如

vector<int> newVector;
newVector.reserve(myRef.size()); //for performance, to avoid realloc
for (vector< int>::iterator it = myRef.begin();it!=myRef.end();++it)
   newVector.push_back(*it);
myRef = newVector;

And after this line

后,这条线

vector< int>& myBackup = myRef

you have a compiler error with redeclaration of myRef...

您有一个编译错误,重新声明myRef…