views:

307

answers:

9

is there a standard in order of functions in cpp file?


there are:

  • global functions
  • constructors
  • destructors
  • getters
  • setters
  • algoritmic functions
  • if qt, slots
  • if a derived class, overrided functions
  • static functions
  • any function type that i can not name...


in cpp file, is there any good way to order?

i order them as i wrote in the list above.

i know that it does not change anything but i care about good looking code...

how do you order?

+2  A: 

Inside of a class, there is no strict rule by the language. Outside of the class you need to ensure that a declaration precedes a definition when the two are separate.

In general, you will find that the team you work with will set forth any format rules regarding source files. This is just aesthetics, however, as it has no effect on the actual execution of the program.

ezpz
+8  A: 

Hi,

my personal order is given by the order inside the class declaration:

class MyClass 
{
public:
    MyClass();
    ~MyClass();

     void start();

protected:
     static void init(MyClass *);

private:
     int  m_iCounter;   ///< counter variable for....
};

would look like this in .cpp:

MyClass::MyClass() :
   m_iCounter(0)
{
   ...
}

MyClass::~MyClass() {
   ...
}

void MyClass::start() {
   ...
}

void MyClass::init(MyClass *) {
    ...
}

The order is defined as followed:

  1. Constructors + Destructors
  2. (only for Qt projects:) signals
  3. public methods - ordered by importance, e.g. first comes start() and stop(), then getters and setters
  4. protected methods ordered by importance
  5. protected members
  6. private methods
  7. private members

Hope that helps.

ciao, Chris

3DH
+4  A: 

This may seem silly, but I try to order my public methods by "order of 'normal' use", so constructors come first, then public doStuff methods, then close methods... the exception to this "rule" is the ~destructor, which comes after the last constructor.

Last comes any private "helper" methods.

I use this same approach in all languages... (C++, Java, C#, Perl, sh, or whatever) and nobody has actually shot-me for it (yet).

Cheers. Keith.

corlettk
+2  A: 

The way I'm used to ordering is from the Symbian platform where the order is:

  • public, protected, private methods
  • public, protected, private variables

The reason for this was guided by rules for extending already released interfaces for backwards compatibility. As the most likely thing to add is a private variable, they are placed at the end of the class so that adding a new one won't change the location of any other variables in the class. Things that change the interface then come before this in the order 'public, protected'. The ordering is then copied for the class methods although these won't change the memory location of any variables in an instance of the class.

And don't ask about the guidelines for virtual functions ;)

workmad3
+1  A: 

From the most important to the lowest:

  • Private Variables (don't kidding, they reveal most of the inner workings of your the class)
  • Constructors
  • Public Methods
  • Protected Methods
  • Private Methods

The methods themselves should be ordered by their "level of abstraction": higher level: up, lower level: down, in other words, structure your methods so that they call only methods below.

Armin
A: 

I use my IDE to goto the functions in my cpp file, and it orders it alphabetically, or i do a search, and with search while you type, this is very fast.

So for me there is absolutely no difference in workflow depending on the order of functions in the .cpp file...

Emile Vrijdags
+2  A: 

Our company's standard is:

  1. constructors
  2. destructors
  3. public methods (sorted alphabetically)
  4. private methors (sorted alphabetically)
Tangurena
I don't link the alphabetical. If you refactor the name, then it could change position. First, it's another thing to remember to do. Second, it involves a more complicated diff in Source Control to figure out what changed.
Robert
It is really rare for us to refactor names like you're suggesting. Mostly because we have to maintain COM compatability for most of our older code (we have products that have been shipping for more than 15 years). Most of the C++ code has been going away, slowly replaced with C# and VB.NET. It is my understanding the last components get retired early next year. The .NET coding standards are different.
Tangurena
A: 

Sometimes it's handy to have a few local helper functions in an unnamed namespace (aka anonymous namespace) within the CPP file. If so, I'd recommend having those functions on top (within the CPP file), just to be sure that they are defined before any other function that would call them.

Niels Dekker
+1  A: 

This is far less important now than it used to be. All decent IDEs these days have (or should have) the ability to go to a definition or reference with a right-click or other simple gesture. Searching through the code is a waste of time.

I typically order them: Constructor Destructor Whatever order I implement the rest

I then go back and group logical/related functionality together

It is probably more important to group related things/order things in a header file for readability than it is in a cpp file.

Tim