Results 1 to 4 of 4

Thread: Methods before Members or Members before Methods?

  1. #1

    Methods before Members or Members before Methods?

    Methods before Members or Members before Methods?

    In a header file, which do you define/declare first. Do you list all your variables before the methods (also known as functions), or do you do it the other way around?

    I always thought that you put the variables first. That was the way I was taught in school. Now I hear that that is wrong and you have to do it the other way. I am told you should list the methods first.

    I know this is all just asthetic. But there must be a set accepted way to do this. What is it? I mean, if you were looking at some sample/example C++ code, how would you expect to see the header files to be written?

  2. #2
    Elite Member TonyT's Avatar
    Join Date
    Jan 2000
    Location
    Fairfax, VA
    Posts
    10,335
    Rule of thumb I believe is put variable first, before functions. It may also depend upon where the code is to be used. For example, in an html doc you should always put the variables first because the browser reads the doc from the top down, in the order inwhich it is writter, same for php.
    No one has any right to force data on you
    and command you to believe it or else.
    If it is not true for you, it isn't true.

    LRH

  3. #3
    Forum Techie Amro's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta
    Posts
    10,665
    It's just convention and will vary from code base to code base. Many companies have conventions they ask their developers to follow to make the code base, as a whole, more maintainable. As Tony said, most of the time members come before methods, but I have seen instances where the opposite was true, by convention.
    amro.co, github.com/amro

  4. #4
    hmmmmmmmmm........
    for example

    Code:
    class x
    {
       public:
            x(int var,int var2)
            :_var(var),_var2(var2) {}
       private:
           int      _var2;
           int      _var;
    }
    when i compile it there's a warning error
    but when u swap the variables var2 to var like

    Code:
    class x
    {
       public:
            x(int var,int var2)
            :_var(var),_var2(var2) {}
       private:
           int      _var;
           int      _var2;
    }

    theres no error

    so if you'll declare the variable first will it make a difference?

Similar Threads

  1. Something for our members passed on?
    By MissTynker2 in forum Feedback, Suggestions & Questions
    Replies: 27
    Last Post: 11-27-06, 05:23 AM
  2. Paging Midwest SG members!
    By ghettoside in forum General Discussion Board
    Replies: 10
    Last Post: 06-05-06, 02:49 PM

Bookmarks

Posting Permissions

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