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

前端 未结 7 1115
孤城傲影
孤城傲影 2020-12-03 05:42

I want to know the detail description on the size of a class. I want to know if there is only data members & member function without any virtual keyword then why the cla

7条回答
  •  心在旅途
    2020-12-03 05:59

    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))

提交回复
热议问题