我想打印出值123456,但它给了我垃圾值。我该如何解决它?你能解释为什么它给出了错误的价值吗?C++,我怎么得不到值“123456”
#include <stdio.h>
#include <stdlib.h>
struct MyInfo
{
private:
int private_key = 123456;
public:
int setkey(int value)
{
private_key = value;
}
int GetScore()
{
return private_key;
}
};
void main()
{
MyInfo* pMyInfo;
pMyInfo = (MyInfo*)malloc(sizeof(MyInfo));
printf("%d\n", pMyInfo->GetScore());
free(pMyInfo);
}
'malloc'只是返回原始内存。没有任何物体会涌入那里。 – StoryTeller
构造函数添加到这个结构 'MyInfo的() \t { \t \t的std ::法院<< “构造方法中调用”; \t}' with ** malloc **不调用构造函数,但** new **关键字可以写** ** private_key的指定值** – Ibrahim