c++ sizeof() of a class with functions

后端 未结 7 919
情深已故
情深已故 2020-12-01 10:38

I have a C++ question. I wrote the following class:

class c
{
    int f(int x, int y){ return x; }
};

the sizeof() of class c returns \"1\"

7条回答
  •  庸人自扰
    2020-12-01 11:24

    Q: Do virtual functions take space on a per-object basis and therefore increase the sizeof an object?

    A: No. The more virtual functions, the larger the vtable. The the more subclasses, the more vtables. If a class has no virtual functions, then there's no need for either a vtable or a (per-object) vtable pointer.

    But none of this affects "sizeof". The functions themselves take a fixed amount of space, regardless.

提交回复
热议问题