Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of functions in cpp file

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?

like image 532
ufukgun Avatar asked Aug 07 '09 12:08

ufukgun


2 Answers

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

like image 136
3DH Avatar answered Nov 16 '22 00:11

3DH


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.

like image 23
corlettk Avatar answered Nov 15 '22 22:11

corlettk