当我遇到以下我不理解的行为时,我正在摆弄各种处理数据的方法。在阅读下面的代码时,请注意变量firstDoubleIter
和secondDoubleIter
,特别是它们指向的列表(各种副本)的哪些元素。
我的问题如下。
#include <list>
#include <iostream>
template <class T>
class MyListContainer
{
public:
std::list<T> list;
MyListContainer() : list() {;}
MyListContainer(const MyListContainer& container) : list(container.list) {;}
MyListContainer(std::list<T> list) : list(list) {;}
std::list<T> getList() const {return list;}
};
int main()
{
typedef MyListContainer<double> DoubleList;
DoubleList doubleContainer = DoubleList(std::list<double>({0, 1}));
auto firstDoubleIter = doubleContainer.getList().begin();
auto secondDoubleIter = ++(doubleContainer.getList().begin());
std::cout << "Using the getList() method\n";
std::cout << "*firstDoubleIter = " << *firstDoubleIter << "\n";
std::cout << "*secondDoubleIter = " << *secondDoubleIter << "\n";
std::list<double> listOfDoubles = doubleContainer.list;
firstDoubleIter = listOfDoubles.begin();
secondDoubleIter = ++listOfDoubles.begin();
std::cout << "Accessing the list directly\n";
std::cout << "*firstDoubleIter = " << *firstDoubleIter << "\n";
std::cout << "*secondDoubleIter = " << *secondDoubleIter << "\n";
}
产生以下输出:
Using the getList() method
*firstDoubleIter = 1
*secondDoubleIter = 0
Accessing the list directly
*firstDoubleIter = 0
*secondDoubleIter = 1
我的理解是,即使使用隐式列表副本, 和 的值*firstDoubleIter
也*secondDoubleIter
应该相同。
我的问题是:为什么它们不一样?