这是一个代码片段:
#include <iostream>
#include <string>
#include <vector>
template<class T>
class Wrapper {
public:
T& t;
explicit Wrapper2( T& obj ) : t(obj) {}
};
class Car {
public:
std::string color;
std::string name;
Car(){}
Car( std::string colorIn, std::string nameIn ) : color( colorIn ), name( nameIn ){}
};
int _tmain( int iNumArguments, _TCHAR* pArgumentText[] ) {
typedef Wrapper<Car> car;
// Create 2 Containers
std::vector<car> collection1;
std::vector<car> collection2;
// Populate collection 1 with reference objects
collection1.push_back( Car("black", "Ford") );
collection1.push_back( Car("white", "BMW") );
collection1.push_back( Car("yellow", "Audi") );
// use referenced object at index 0 in collection 1
// to populate index 0 of collection 2
collection2.push_back( collection1[0] );
// Print Out index 0 of collection2's referenced object's fields
std::cout << collection2[0].t.color << " " << collection2[0].t.name << std::endl;
// Change collection2's index 0 referenced object's fields
collection2[0].t.color = std::string( "green" );
collection2[0].t.name = std::string( "Gremlin" );
// Print out collection1's index 0 referenced object's fields
// and they are changed
std::cout << collection1[0].ptr->color << " " << collection1[0].ptr->name << std::endl;
return 0;
}
这确实在 MSVS2015 CE 中成功编译、构建和运行,没有任何错误。
这是否会产生未定义的行为,如果是的话;怎么办?
最佳答案
Will creating a class template wrapper of a reference cause undefined behavoir?
没有。
然而,滥用引用(以及指针和迭代器)通常会造成这种情况。如果引用的对象被销毁,则该引用将悬空。这发生在所有引用上,而不仅仅是那些包装在类中的引用——包装器没有效果。当引用悬挂时,使用它具有未定义的行为。
collection1.push_back( Car("black", "Ford") );
在这里,对象是临时的。该对象一直存在到 push_back
结束。之后, vector 中包装器中的引用是悬空的。
std::cout << collection2[0].t.color << " " << collection2[0].t.name << std::endl;
这里你使用了悬空引用,程序有未定义的行为。
这是使用没有 UB 的包装器的示例:
Car c;
collection1.push_back(c);
collection1[0].color = "black";
关于c++ - 创建引用的类模板包装器会导致未定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41483552/