私は次の問題があります。以下のコードでは、オブジェクト変数pのアドレスは、最初のメンバー 'a'のアドレスと同じです。しかし、私はpとaの値を両方とも表示します。どのように同じ住所の場所に2つの異なる値がありますか?同じアドレスの場所はどのように2つの異なる値を与えますか?
class sample {
public:
int a;
int b;
int c;
//sample();
sample(int x,int y, int z){
a=x;
b=y;
c=z;
}
};
int main() {
//sample p;
//sample sample(2,3,4);
sample p = sample(2,3,4);
// cout<<"The value that is stored in the z is "<< p <<endl;
printf("The value of the object handle p is %d \n",p);
printf("The address of the object handle p is %d \n",&p);
//p->a =2;
//p->b =3; This could be accessesd in the later part of the code.
//p->c =4;
printf("The address of a is %d\n",&(p.a));
printf("The value stored in the a is %d\n",p.a);
printf("The value stored in the b is %d\n",p.b);
printf("The value stored in the c is %d\n",p.c);
}
上記のコードの出力は次のとおり
The value of the object handle p is 2358832
The address of the object handle p is 2358848
The address of a is 2358848
The value stored in the a is 2
The value stored in the b is 2358852
The value stored in the c is 2358856
--------------------------------
Process exited after 0.2105 seconds with return value 0
Press any key to continue . . .
通常、最初のprintfは実行時エラーを引き起こします。コンパイラは、途中でコンパイルしながらそれぞれの警告を出す必要があります。 – dmi
あなたは仮定が偽です。第1の 'printf'は期待どおりに動作しません。 (オブジェクトpをスタックに置き、最初の 'sizeof(int)'バイトを 'int'値として読み込みます)' printf'は可変引数を使用しますが、**型安全ではありません。代わりに 'std :: cout'でこれを試してください。それで、それはおそらくコンパイルされません。 ( 'sample'クラスの出力演算子がないためです。) – Scheff
いいえ、コンパイルエラーは発生しませんでした。ターミナルからの出力をそのまま貼り付けました。ところで、私はdev C++を使用しており、コンパイラはTDM-GCC 4.9.2 64ビットリリースです。 –