Coding style

This is a discussion on Coding style within the C++ Programming forums, part of the General Programming Boards category; I would like to hear some thoughts about two different ways to code. Would you put all variables int the ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Coding style

    I would like to hear some thoughts about two different ways to code.
    Would you put all variables int the beginning of the class and after that the functions, or do you mix them around?

    Lets say you have a function that uses a list of integers. That list is only used by that function. Would you do
    Code:
    class A
    {
       int a;
       int b;
       int list[20];
       ....
       void fun1();
       void fun2();
       void listFun();
       ...
    };
    or
    Code:
    class A
    {
       int a;
       int b;   
       ....
       void fun1();
       void fun2();
       int list[20];
       void listFun();
       ...
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by C_ntua
    Would you put all variables int the beginning of the class and after that the functions, or do you mix them around?
    Generally, I declare the public members of a class first, followed by protected members, followed by private members. Since member variables are usually private, they are thus declared towards the end.

    The rationale is that everyone interested in the class would normally be interested in the public interface, but only those wanting to inherit from the class would also be interested in the protected interface, and only those needing to maintain the class would also be interested in the private implementation, hence this presents the parts of the class that the most people would be normally interested in first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it bad style of coding ??
    By noobcpp in forum C++ Programming
    Replies: 15
    Last Post: 11-06-2008, 09:39 AM
  2. Your Coding Style?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 45
    Last Post: 06-02-2005, 08:19 AM
  3. Coding style?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 10-22-2003, 11:26 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. coding style
    By ActionMan in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2001, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21