More than 1 address for derived class object?

后端 未结 2 1051
醉话见心
醉话见心 2021-01-01 23:48

In Item 27 of \"Effective C++\" (3rd Edition, Page 118), Scott Meyers said:

class Base { ... };
class Derived: public Base { ... };
Derived d;
Base *pb = &am         


        
2条回答
  •  天命终不由人
    2021-01-02 00:16

    Just try it:

    class B1
    {
        int i;
    };
    
    class B2
    {
        int i;
    };
    
    class D : public B1, public B2
    {
        int i;
    };
    
    int
    main()
    {
        D aD;
        std::cout << &aD << std::endl;
        std::cout << static_cast( &aD ) << std::endl;
        std::cout << static_cast( &aD ) << std::endl;
        return 0;
    }
    

    There's no possible way for the B1 sub-object to have the same address as the B2 sub-object.

提交回复
热议问题