user3243499
user3243499

Reputation: 3151

Will there any performance impact of a Class definition structure order in C++?

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.

Upvotes: 0

Views: 84

Answers (2)

eerorika
eerorika

Reputation: 238311

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.

Upvotes: 1

idmean
idmean

Reputation: 14865

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.

Upvotes: 3

Related Questions