sizeof *this object

后端 未结 3 1981
野性不改
野性不改 2021-02-19 10:39

Code:

#include 

class myc {
    int dummy;
public:
    int si(){return sizeof(*this);}
};

class d_myc : public myc {
    int d_dummy;
};

int mai         


        
3条回答
  •  梦毁少年i
    2021-02-19 11:22

    I do not know "Why were your expectations wrong" because I cannot read minds :). But if you wrote sizeof(myc), sizeof(d_myc) the compiler would generate exactly the same code as for what you have coded above. myc has 1 int, assuming 32bit, so 4 bytes. d_myc has 2 ints => 8 bytes.

提交回复
热议问题