次のコードは、出力を136として示しています。しかし、最初の2つのアドレス比較がどのように等しいか理解できませんでした。これを理解する助けを感謝します。ありがとうございました。reintepret_castでオブジェクトアドレスを解釈する
#include <iostream>
class A
{
public:
A() : m_i(0){ }
protected:
int m_i;
};
class B
{
public:
B() : m_d(0.0) { }
protected:
double m_d;
};
class C : public A, public B
{
public:
C() : m_c('a') { }
private:
char m_c;
};
int main()
{
C d;
A *b1 = &d;
B *b2 = &d;
const int a = (reinterpret_cast<char *>(b1) == reinterpret_cast<char *>(&d)) ? 1 : 2;
const int b = (b2 == &d) ? 3 : 4;
const int c = (reinterpret_cast<char *>(b1) == reinterpret_cast<char *>(b2)) ? 5 : 6;
std::cout << a << b << c << std::endl;
return 0;
}
なぜここで 'reinterpret_cast'を使用していますか?あなたのアーキテクチャのポインタ型に 'char * 'を使うと何が期待されますか? –
これはリアルタイムアプリケーションからのものではありません。これはC++テストからのものです。私はreinterpret_castがこの特定のケースでどのようにアドレスを解釈しているかを理解したいだけです。 – irappa