ufukgun
ufukgun

Reputation: 7209

order of functions in cpp file

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


there are:


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?

Upvotes: 8

Views: 4006

Answers (9)

Armin
Armin

Reputation: 1062

From the most important to the lowest:

  • Private Variables (not 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.

Upvotes: 2

Emile Vrijdags
Emile Vrijdags

Reputation: 1578

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...

Upvotes: 1

Tim
Tim

Reputation: 20360

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.

Upvotes: 3

Niels Dekker
Niels Dekker

Reputation: 51

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.

Upvotes: 1

Tangurena
Tangurena

Reputation: 2131

Our company's standard is:

  1. constructors
  2. destructors
  3. public methods (sorted alphabetically)
  4. private methors (sorted alphabetically)

Upvotes: 2

3DH
3DH

Reputation: 1471

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

Upvotes: 11

workmad3
workmad3

Reputation: 25677

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 ;)

Upvotes: 3

corlettk
corlettk

Reputation: 13574

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.

Upvotes: 4

ezpz
ezpz

Reputation: 12037

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.

Upvotes: 2

Related Questions