でのポインタの値を設定します。は、私はそうのような構造体を持つ構造体
私は単純に行くi
にint* p1 = new int;
ポイントをしたい場合:p1 = &i;
にはどうすればi
へfoo.bar
ポイントを作るのですか?
私は、コンストラクタは次のことを行う必要があると思う:
foo::foo() {
bar = new int;
}
しかし、私はi
へfoo.bar
ポイントを作る方法がわかりません。
でのポインタの値を設定します。は、私はそうのような構造体を持つ構造体
私は単純に行くi
にint* p1 = new int;
ポイントをしたい場合:p1 = &i;
にはどうすればi
へfoo.bar
ポイントを作るのですか?
私は、コンストラクタは次のことを行う必要があると思う:
foo::foo() {
bar = new int;
}
しかし、私はi
へfoo.bar
ポイントを作る方法がわかりません。
私はこれを解決しました。
複数の人がメモリリークについて正しくありました。構造体のポインタ部材を欽慕する
typedef struct foo {
int *bar;
} foo;
:
int main() {
foo fooIn;
int i = 2;
fooIn.bar = &i; // have the int pointer 'bar' point to the value i;
int *barVal = (int*)fooIn.bar;
printf("bar points to: %d, *barVal) // prints "bar points to: 2"
}
実際の例では、construtor "foo()"を初期化バーとそれを削除する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. –
'foo-に有効ではないので、これは間違いC.関数ではありません> bar =&i; ' –
このCまたはC++には質問がありますか?あなたの構造体はCで書かれているのに対し、 'new'キーワードはC++です。 – plasmacel