开发者

Is the order of declarations within a class important?

Can somebody please explain the order of private and public in a class. Is it important or not?

For example:

class Account{
public:
    Account(str开发者_JAVA技巧ing firstName, string lastName, int id);
    void printAccount();
private:
    string strLastName;     
    string strFirstName;    
};

Will it be the same as:

class Account{
private:
    string strLastName;     
    string strFirstName; 
public:
    Account(string firstName, string lastName, int id);
    void printAccount();
};


It doesn't affect program behavior, but perhaps does affect readability. There's a convention (that you are free to not follow of course) that public stuff must be listed first so that the reader sees the class interface (what other classes and functions can do with the class) earlier.


It's important for the visibility of class members

class Account{
public:
    Account(string firstName, string lastName, int id);
    void printAccount();

    Foo makeFoo(); // invalid! Foo not declared yet
    typedef Foo foo_type; // invalid! Foo not declared yet

private:
    string strLastName;     
    string strFirstName;
    class Foo { };    
};

You can only refer to a not yet declared name in a parameter, default argument, function body or constructor initialization list in class (or such things in nested classes). In other cases, you can't and you have to arrange declarations such that they are visible.


In your specific example, the order is not important. Keep in mind however, that members are initialized in the order in which they are declared.


While in the general case it is more a question of style, there are subtle differences, if you have member attributes with more than one access level, in:

  • initialization order of members
  • (potentially) memory layout of the object

The order of initialization of members in the constructor initialization list is the same order in which the attributes appear in the class definition, so if you place public attributes before private attributes all public attributes will be initialized before any private attribute (this will really affect code only if you use some attribute as an argument to the constructor of a different attribute).

Potentially, the memory layout of the object may differ. All fields with the same access level are lay out in memory in the same order in which they are declared in the class. The compiler is free to reorder different access levels as it wish (i.e. lay out in memory public before protected before private...) but many don't. The memory layout of the object will be different if you reorder the access levels and there are fields in more than one access level --unless the compiler reorders member attributes.


Both express the same.


The order is unimportant if you specify the accessibility explicitly. If becomes significant if you don't, because the default is "private".


It is just a matter of taste, make up some convention and use is consequently in your application.

Not that private is default so you can omit the private keyword is you declare private variables first.

class Account{
/*private:*/ // can be left out since private is default
    string strLastName;     
    string strFirstName; 
public:
    Account(string firstName, string lastName, int id);
    void printAccount();
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜