CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    should the class constructor be defined before or after public member declarations?

    Simple question this time. I was reordering some classes to get rid of annoying "will be initialized after" warnings and it suddenly didn't make any sense to me that I was declaring my public member variables after the class constructor. I have always done it like that but it makes more sense to declare variables first and then initialize there values.

    Code:
    // class definition
    class my_class {
    
    public:
    
       // member variable
       bool my_bool;
    
       // constructor
       my_class() : my_bool(false) { }
       
    };
    This seems to make more sense than the other way around so why did I always do it in the other order?

    Does it make any difference in general, or in any specific cases?

    LMHmedchem

  2. #2
    Join Date
    Aug 2006
    Posts
    231

    Re: should the class constructor be defined before or after public member declaration

    The C++ Core Guidelines recommends (and enforces) the following order:

    constructors, assignments, destructor
    functions
    data

    That's good for readability and consistency, but doesn't make any difference other than that.

    By the way, you can initialize the member variable directly in the class declaration:

    Code:
    class my_class
    {
    public:
       // constructor
       my_class() = default;
    
       // member variable
       bool my_bool = false;
    };
    Last edited by TubularX; August 14th, 2018 at 12:42 PM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: should the class constructor be defined before or after public member declaration

    This is a matter of taste really. However, the 'accepted' practice is that the class functions come before the class variables where possible (I can't remember if there were any c++98 restrictions as it's too long ago since I used that version!) because when looking up a class to see its definitions you're usually more interested in its functions than in its variables - hence these go first to be more easily found. In any case, class variables should usually be private and not public.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: should the class constructor be defined before or after public member declaration

    Quote Originally Posted by TubularX View Post
    The C++ Core Guidelines recommends (and enforces) the following order:

    constructors, assignments, destructor
    functions
    data

    That's good for readability and consistency, but doesn't make any difference other than that.

    By the way, you can initialize the member variable directly in the class declaration:

    Code:
    // class definition
    class my_class {
    
    public:
       // constructor
       my_class() = default;
    
       // member variable
       bool my_bool = false;
    };
    Not in c++98 which the OP uses!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Aug 2006
    Posts
    231

    Re: should the class constructor be defined before or after public member declaration

    Quote Originally Posted by 2kaud View Post
    Not in c++98 which the OP uses!
    I'm sorry, I did not know. Did I miss something? There is no mention of C++98. I was just trying to give some helpful advice.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: should the class constructor be defined before or after public member declaration

    Quote Originally Posted by TubularX View Post
    I'm sorry, I did not know. Did I miss something? There is no mention of C++98. I was just trying to give some helpful advice.
    No, the OP didn't say in this thread but has in some of his other threads. For some reason he's reluctant to move to a newer compiler - despite suggestions that he should..... I've almost forgotten the c++98 limitations - it's that long ago since I last used it!
    Last edited by 2kaud; August 15th, 2018 at 03:14 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Feb 2017
    Posts
    677

    Re: should the class constructor be defined before or after public member declaration

    Quote Originally Posted by LMHmedchem View Post
    it makes more sense to declare variables first and then initialize there values.
    Personally I put everything in the order I assume will be most helpful for users of my code to understand it. I also try to be consistent and always do the same.

    This usually coincides with more rule-based approaches like say The Core C++ Guidelines by Stroustrup & Sutter. The naming and layout rules are found in the NL section,

    https://github.com/isocpp/CppCoreGui...eGuidelines.md
    Last edited by wolle; August 16th, 2018 at 12:34 AM.

  8. #8
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: should the class constructor be defined before or after public member declaration

    Quote Originally Posted by 2kaud View Post
    No, the OP didn't say in this thread but has in some of his other threads. For some reason he's reluctant to move to a newer compiler - despite suggestions that he should..... I've almost forgotten the c++98 limitations - it's that long ago since I last used it!
    The main reason I still use c++98 (and sometimes still gcc3) is that I often have to work on a very large program that was originally written in F66 (on IBM punch cards) and then revised when F77 came out. I don't think it was ever updated to F90 or F95. Much later, the code received a c++ wrapper so that the original Fortran code is just a function called by c++. This makes it easier for the F77 code to talk to the more modern world and mainly replaced the dreadful F77 IO functions with something more friendly like getline() (instead of having to specify each individual input character on input and output lines). There can be significant challenges when I need to make changes and recompile and I need to stick with a set of compilers and runtime components that will work with all of the code. Since I use older compilers for that project, I tend to stick with it because I don't have much experience with anything newer.

    What I am working on now is not Frankenstein code and I could use something different. I think I would just have to add some instructions to my make file to tell it to use a different compiler and line to different libraries.

    So my understanding about the order is that it doesn't make any functional difference other than the need to declare a before b if the declaration of b depends on a. Most folks just stick with a conventional order that makes it easier to read and document.

    Thanks for the assistance.

    LMHmedchem
    Last edited by LMHmedchem; August 16th, 2018 at 11:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured