Home > Blockchain >  Will there any performance impact of a Class definition structure order in C ?
Will there any performance impact of a Class definition structure order in C ?

Time:12-15

I have two classes as follows (C ):

class A {
private:
    int v1;
    char c;
    long long v2;
    double v3;
    bool f1();
    bool f2(int x);
    void f3(int y);
    struct Node tempNode;
public:
    A();
};


class B {
private:
    int v1;
    char c;
    long long v2;
    double v3;
    struct Node tempNode;
    bool f1();
    bool f2(int x);
    void f3(int y);
    
public:
    B();
};

Class B just has struct Node tempNode declaration moved from after functions declarations in A to all members declarations first then methods in class B.

My doubt is, will class B more efficient than class A simply due to this reordering? or they will perform at the same level. Let me know even if the gains in B is very small.

CodePudding user response:

No, both classes will be laid out exactly the same in memory. Thus there's no performance difference.

Note that (non-virtual) member functions do not affect the layout of the data in memory. If you disregard them, you can easily see that both classes have the same layout.

CodePudding user response:

will class B more efficient than class A simply due to this reordering?

No. Order of member functions in relation to data members or to each other has no effect on the program.

  •  Tags:  
  • c
  • Related