Я действительно не понимаю результат этого кода:
struct exampleClass
{
double a = 12.1;
std::map<int, double*> innerMap;
exampleClass()
{
std::cout << "ADR of variable a in constructor = " << &a << std::endl;
std::cout << "ADR of this in constructor = " << this << std::endl;
innerMap.emplace(4,&a);
}
};
int main(){
std::map<int, exampleClass> map;
map.insert(std::make_pair(4, exampleClass{}));
for (auto& it : map)
{
std::cout << "ADR of a = " << &(it.second.a) << std::endl;
std::cout << "Content of a = " << it.second.a << std::endl;
std::cout << it.second.innerMap.find(4)->second << std::endl;
std::cout << *(it.second.innerMap.find(4)->second); //the output is wrong
}
}
Результат:
ADR of variable a in constructor = 0x7ffee080b500
ADR of this in constructor = 0x7ffee080b500
ADR of a = 0x559ddb2c42e8
Content of a = 12.1
0x7ffee080b500
6.95312e-310
Я не понимаю, почему адреса в цикле for
отличаются от адресов в конструкторе.Также «адрес конструктора» используется для вставки в innerMap
, что вызывает ошибку.
Может ли кто-нибудь объяснить мне это?
Что меня смущает, так это следующееожидается:
exampleClass abc{};
std::cout << "ADR of a = " << &(abc.a)<< std::endl;
std::cout << *(abc.innerMap.find(4)->second) << std::endl;