return by reference/pointer 的一个静态局部变量
return by reference/pointer of a static local variable
我很困惑为什么如果我通过引用获取静态 unordered_map 会被清除但如果我通过指针获取它则不会...(您可以在此处执行代码:http://cpp.sh/4ondg )
是否因为当引用超出范围时,它的析构函数被调用?如果是这样,那么第二个 get 函数会得到什么?
class MyTestClass {
public:
static std::unordered_map<int, int>& getMap() {
static std::unordered_map<int, int> map;
return map;
}
static std::unordered_map<int, int>* getMapByPointer() {
static std::unordered_map<int, int> map;
return ↦
}
};
int main()
{
// By reference
{
auto theMap = MyTestClass::getMap();
std::cout << theMap.size() << std::endl;
theMap[5] = 3;
std::cout << theMap.size() << std::endl;
}
{
auto theMap = MyTestClass::getMap();
std::cout << theMap.size() << std::endl;
theMap[6] = 4;
std::cout << theMap.size() << std::endl;
}
// By pointer
{
auto theMap = MyTestClass::getMapByPointer();
std::cout << theMap->size() << std::endl;
(*theMap)[5] = 3;
std::cout << theMap->size() << std::endl;
}
{
auto theMap = MyTestClass::getMapByPointer();
std::cout << theMap->size() << std::endl;
(*theMap)[6] = 4;
std::cout << theMap->size() << std::endl;
}
}
当你这样做时
auto theMap = MyTestClass::getMap();
theMap
的类型被推断为 std::unordered_map<int, int>
– 不是引用。因此,函数调用返回的引用被复制到局部变量theMap
中;当您修改 theMap
时,您只是在修改此副本。
要存储引用,将其声明为 auto&
:
auto& theMap = MyTestClass::getMap();
然后您将按预期修改原始对象。
您在将静态地图分配给局部变量时无意中复制了它。在:
auto theMap = MyTestClass::getMap();
auto
被推断为 std::unordered_map<int, int>
,而不是 std::unordered_map<int, int>&
,这会导致从返回的引用中复制初始化一个新对象。所以,theMap
是一个完全独立于静态地图的对象。
指针版本没有这个问题,因为类型被推导为指针,所以唯一被复制的是指针值本身,它总是指向同一个(静态)对象。
我很困惑为什么如果我通过引用获取静态 unordered_map 会被清除但如果我通过指针获取它则不会...(您可以在此处执行代码:http://cpp.sh/4ondg )
是否因为当引用超出范围时,它的析构函数被调用?如果是这样,那么第二个 get 函数会得到什么?
class MyTestClass {
public:
static std::unordered_map<int, int>& getMap() {
static std::unordered_map<int, int> map;
return map;
}
static std::unordered_map<int, int>* getMapByPointer() {
static std::unordered_map<int, int> map;
return ↦
}
};
int main()
{
// By reference
{
auto theMap = MyTestClass::getMap();
std::cout << theMap.size() << std::endl;
theMap[5] = 3;
std::cout << theMap.size() << std::endl;
}
{
auto theMap = MyTestClass::getMap();
std::cout << theMap.size() << std::endl;
theMap[6] = 4;
std::cout << theMap.size() << std::endl;
}
// By pointer
{
auto theMap = MyTestClass::getMapByPointer();
std::cout << theMap->size() << std::endl;
(*theMap)[5] = 3;
std::cout << theMap->size() << std::endl;
}
{
auto theMap = MyTestClass::getMapByPointer();
std::cout << theMap->size() << std::endl;
(*theMap)[6] = 4;
std::cout << theMap->size() << std::endl;
}
}
当你这样做时
auto theMap = MyTestClass::getMap();
theMap
的类型被推断为 std::unordered_map<int, int>
– 不是引用。因此,函数调用返回的引用被复制到局部变量theMap
中;当您修改 theMap
时,您只是在修改此副本。
要存储引用,将其声明为 auto&
:
auto& theMap = MyTestClass::getMap();
然后您将按预期修改原始对象。
您在将静态地图分配给局部变量时无意中复制了它。在:
auto theMap = MyTestClass::getMap();
auto
被推断为 std::unordered_map<int, int>
,而不是 std::unordered_map<int, int>&
,这会导致从返回的引用中复制初始化一个新对象。所以,theMap
是一个完全独立于静态地图的对象。
指针版本没有这个问题,因为类型被推导为指针,所以唯一被复制的是指针值本身,它总是指向同一个(静态)对象。