#include "stdafx.h"
#include "stdio.h"
struct TestStr
{
int id;
void SetId(int id_new){id = id_new;}
};
int main(int argc, char* argv[])
{
const TestStr ts = {1};
const TestStr* p_ts = &ts;
((TestStr)(*p_ts)).SetId(9);
printf("TestStr::id = %d\r\n",ts.id);
return 0;
}
id還是等于1,不信試試,順便替換成下面這一句再試試看
((TestStr&)(*p_ts)).SetId(9);
另外還有一個奇怪的問題:
((TestStr)(*p_ts)).SetId(9); //編譯通過
((TestStr)(*p_ts)).id = 9; //編譯不通過
(&((TestStr)(*p_ts)))->id = 9; //編譯通過
誰可以解釋一下原因?不知。