为什么这段代码不起作用:
int main () {
Bob bob;
bob.giveANewFredSomeData();
Fred::sharedFred.getANumber(); //Crashes here due to someData inside fred being NULL
return 0;
}
Class Data {
int aNumber;
}
Class Bob {
void giveANewFredSomeData{
Data data;
Fred::sharedFred = new Fred(data);
}
}
Class Fred {
Data someData;
static sharedFred;
Fred (Data data) {
someData = data;
}
int getANumber(){
return someData.aNumber
}
}