Multiple Inheritance question
-
class CBase {
string id;
public:
void show() {
cout << id << endl;
}
};class CDerive1 : public CBase { };
class CDerive2 : public CBase { };
class CSon : public CDerive2, public CDerive1 { };int main ( )
{
CSon s;
cout << &s << endl;
cout << "---------" << endl;CDerive1 \*pd1 = &s; cout << pd1 << " &S: " << &s << endl; CDerive2 \*pd2 = &s; cout << pd2 << " &S: " << &s << endl; cout << "---------" << endl;
The output of this code is : 0035FB20 --------- 0035FB3C 0035FB20 -------- don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.
-
class CBase {
string id;
public:
void show() {
cout << id << endl;
}
};class CDerive1 : public CBase { };
class CDerive2 : public CBase { };
class CSon : public CDerive2, public CDerive1 { };int main ( )
{
CSon s;
cout << &s << endl;
cout << "---------" << endl;CDerive1 \*pd1 = &s; cout << pd1 << " &S: " << &s << endl; CDerive2 \*pd2 = &s; cout << pd2 << " &S: " << &s << endl; cout << "---------" << endl;
The output of this code is : 0035FB20 --------- 0035FB3C 0035FB20 -------- don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.
-
class CBase {
string id;
public:
void show() {
cout << id << endl;
}
};class CDerive1 : public CBase { };
class CDerive2 : public CBase { };
class CSon : public CDerive2, public CDerive1 { };int main ( )
{
CSon s;
cout << &s << endl;
cout << "---------" << endl;CDerive1 \*pd1 = &s; cout << pd1 << " &S: " << &s << endl; CDerive2 \*pd2 = &s; cout << pd2 << " &S: " << &s << endl; cout << "---------" << endl;
The output of this code is : 0035FB20 --------- 0035FB3C 0035FB20 -------- don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.
don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.
It is surprise of Multiple Inheritance. CSon consist of 2 blocks: CDerive2 block and CDerive1 block, each of them has own address. [CDerive2][CDerive1] or [ C S o n ] Therefore address of CSon instance is equal to address of CDerive2 part of CSon, but is not equal to address of CDerive1 part of CSon.
With best wishes, Vita
-
don't know why cout << pd1, and cout << pd2 are different, they are assigned the same value &s.
It is surprise of Multiple Inheritance. CSon consist of 2 blocks: CDerive2 block and CDerive1 block, each of them has own address. [CDerive2][CDerive1] or [ C S o n ] Therefore address of CSon instance is equal to address of CDerive2 part of CSon, but is not equal to address of CDerive1 part of CSon.
With best wishes, Vita