下面的虛擬程序模仿我正在排除故障的另一個程序的行爲。爲什麼方法範圍結束後矢量內容發生了變化?
#include <iostream>
#include <vector>
class A
{
public:
std::vector<int> data;
void DoTheThing()
{
while (data.size() < 10) {
data.push_back(1);
}
}
};
class B
{
public:
std::vector<A> objs;
B()
{
A one, two, three;
objs.push_back(one);
objs.push_back(two);
objs.push_back(three);
}
void DoTheThing()
{
for (auto obj: objs) {
obj.DoTheThing();
std::cout << "DEBUG length during=" << obj.data.size() << std::endl;
}
}
};
int main()
{
B b;
b.DoTheThing();
for (auto obj : b.objs) {
std::cout << "DEBUG length after=" << obj.data.size() << std::endl;
}
}
予編譯和作爲運行:
$ g++ -Wall --std=c++11 -o test test.cpp
$ ./test
DEBUG length during=10
DEBUG length during=10
DEBUG length during=10
DEBUG length after=0
DEBUG length after=0
DEBUG length after=0
$
出於某種原因,A
物體在b
的objs
向量狀態在b.DoTheThing()
呼叫和隨後的打印語句之間改變。我的問題是發生了什麼? A
對象data
矢量以某種方式超出範圍並被刪除,或者可能是整個A
對象?這似乎是一個範圍問題 - 可能甚至是一個簡單的問題 - 但它已經足夠長,因爲我用C++編程,我不確定。在其他方法中調用b.DoTheThing()
後,如何使data
向量的內容保持不變?
複製構造函數'explicit'可能會導致其他問題。最好只學習語言以瞭解製作副本的位置,並在需要時避開它。 –
@JonathanWakely,其中一個已知問題是RVO的事情。在某些情況下可以解決這個問題,我已經在答案中更新了。是的,「顯性」有其自身的侷限性,但我認爲它們值得。幾周前我已經遇到過這種情況,調試真的很困難。我們也可以在它下面有一個'#ifdef /#endif'並創建一個'#define conditional_explicit explicit'。這對最終檢查任何這樣的副本並恢復正常很有用。 – iammilind