Why the output of codes below is an invalid string? If lines #1 to #3 cannot be changed, how can I assign the value of config.strConfig
to data2->str
?
// classA.h
#pragma once
#include <string>
class Config {
public:
int a;
std::string strConfig;
};
// structA.h
#pragma once
#include <string>
struct Data {
char test[2];
std::string str;
};
// main.cpp
#include "classA.h"
#include "structA.h"
#include <iostream>
int main() {
Data data; // #1
memset(&data, 0, sizeof(data)); // #2
Data* data2 = &data; // #3
Config config;
config.strConfig = "aaaa";
data2->str = config.strConfig;
std::cout << (data2->str) << std::endl;
}