Reputation: 7209
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
Reputation: 1062
From the most important to the lowest:
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
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
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
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
Reputation: 2131
Our company's standard is:
Upvotes: 2
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:
signals
start()
and stop()
, then getters and settersHope that helps.
ciao, Chris
Upvotes: 11
Reputation: 25677
The way I'm used to ordering is from the Symbian platform where the order is:
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
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
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