sizeof *this object

后端 未结 3 1995
野性不改
野性不改 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条回答
  •  一生所求
    2021-02-19 11:35

    This is resolved at compile time:

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

    i.e. *this is always myc and will never be d_myc. To get what you want you will have to override the function in d_myc to do the same in the derived as the base. This is because sizeof(d_myc) includes the base class too.

提交回复
热议问题