How do you determine the size of an object in C++?

前端 未结 11 1010
轮回少年
轮回少年 2020-11-28 05:38

For example, say I have a class Temp:

class Temp
{
    public:
        int function1(int foo) { return 1; }
        void function2(int bar) { foobar = bar; }         


        
11条回答
  •  心在旅途
    2020-11-28 06:19

    The size of an object of a class is equal to the sum of the sizes of all the data members of that class. For example if I have a class

    class student
    {
    private:   
       char name[20];
       int rollno, admno;
       float marks;
    
    public:
      float tmarks, percentage;
    
      void getdata();
    
      void putdata();
    };
    

    Now, if I make an object of this class, say s1, then the size of this object will be 36 bytes:

    [20(name)+2(rollno)+2(admno)+4(marks)+4(tmarks)+4(percentage)]
    

提交回复
热议问题