Why class size depend only on data members and not on member functions?

Deadly 提交于 2019-11-27 14:03:20

Because the class's functions are not saved inside the object itself. Think of it in terms of C programming, every function of class A takes one secret parameter, the this pointer, so in actuality they are just functions with one extra parameter.

For example imagine it like this:

int display(A* thisptr)
{
   //do something
   printf("%d",thisptr->a); 
   return;
}

So the display function is saved as just a simple function with one extra parameter. The name though is mangled depending on the compiler.

I believe that different rules apply for virtual functions which would involve function pointers but since I am not sure maybe someone else can enlighten us on this matter.

This is implementation dependent - it's not specified in the standard. However, you are right, non-virtual member functions (and even virtual functions after the first one) don't affect class size.

That's because it would use a lot of memory if every instance of the class would have pointers to all functions. And why would they? At runtime, the object knows what type it is, and it can call the appropriate function. And the same function is identical across instances, all that's different is the object it operates on, which is passed as a parameter under the hood.

Functions/methods are stored as code, not as data. Just like one executable is stored as single file, and may be launched multiple times - and multiple instances of it would have different data. Similarly, a function is and executable code, and data you pass to it can be different (like different documents, for same word-processing software).

Executable code will not have any sizeof, as they don't take space on stack or heap, while program is running. It is stored itself in the executable image, and is loaded once by the OS, when you launch the program.

Just like a regular C function, a C++ method is just an address in memory for the execution to jump to when called. The only difference is the first parameter, which is a pointer to the object from which you are calling the function.

Except for hidden data members introduced to implement virtual functions and virtual inheritance, the instance size is solely determined by a class’s data members and base classes. From C++: Under the Hood (1994) In case you want to know more.

Member functions are part of text segment of a process and objects are part of data segment of the process. So as objects are data only, it calculate size by adding all the sizes of data members of a class. Member functions are common for all the objects, it only differ by the first argument of the particular objects pointer (called this pointer) which is passed as the hidden pointer to every member function of the class.

Because the function state leaves in the stack, and gets created / deleted upon function entering / exiting.

In the size of the object there are just the members that contributes to the storage of the object state. A function is just a piece of code that starts at a given point that does not depend on the particular object instance it refers to.

Think to

A a1, a2;

a1.a and a2.a are different, but a1.display() and a2.dispaly() is the same function code (think to int A::display()) as int display(A* this))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!