我想打印出值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