在一個真實的例子中,你應該讓construtor「foo()」初始化bar和一個刪除它的dtor; 這只是一個例子,如何使用指針結構來訪問成員:
#include <iostream>
using namespace std;
typedef struct foo
{
int* bar;
foo(){}
}FOO, *PFOO;
int main(int argc, wchar_t *argv[])
{
struct foo* ptrFoo = new foo;
// PFOO pFoo = new foo; // you can use this also
ptrFoo->bar = new int(10);
cout << ptrFoo->bar << endl;
cout << (*ptrFoo).bar << endl;
cout << *ptrFoo->bar << endl;
cout << *(*ptrFoo).bar << endl;
delete ptrFoo->bar;
ptrFoo->bar = NULL;
delete ptrFoo;
ptrFoo = NULL;
std::cin.get();
return 0;
}
這絕對不是C.功能作爲一個結構內的構件在C. –
'foo-無效> bar =&i;' –
這是C還是C++的問題?因爲你的結構是用C編寫的,而'new'關鍵字是C++。 – plasmacel