我有以下代码片段,其中我试图在std :: map中存储子结构childStruct
,其中数据部分是父结构parentStruct
。但是,当我尝试再次检索子项variable2
的数据部分时,我得到了错误的值。
以下是代码:
// Example program
#include <iostream>
#include <string>
#include <map>
struct parentStruct
{
uint32_t variable1;
};
struct childStruct : parentStruct
{
uint32_t variable2;
};
int main()
{
struct childStruct test;
std::map<uint32_t, struct parentStruct> m_list;
test.variable1 = 50;
test.variable2 = 100;
m_list[1] = test;
struct childStruct *test2 = static_cast<struct childStruct *> (&m_list.at(1));
std::cout << test2->variable1 << std::endl;
std::cout << test2->variable2 << std::endl;
}
上一个程序的输出是:
50
138353
而不是:
50
100
如何才能存储variable2
中存储的正确值。