I wonder if the declaration order of a function in a header has an importance.
Let's imagine : I've got two projects that use the same header definition, and the header had to be copied for some obscure reason. And these headers are not the same in terms of declaration function order.
So the header for my first project would be :
class A {
someFunctionA();
someFunctionB();
}
and the header in the second project :
class A {
someFunctionB();
someFunctionA();
}
And now what will happen if I use the implementation created in the first project in my second project (like a dynamic library or whatever) ?
I'm aware that I should use the same header definition for both project, I was just wondering if the compiler will use some sort of stack for the functions or will reorder them.
Even if you're sharing the same header file, you can still come up against this issue with versioning. For example, your project might have been compiled against version 1 of the dynamic library. You then ship version 2 of the library without recompiling your project. In that situation, you need to ensure the library does not break binary compatibility.
A good list of what can and can't be done in maintaining binary compatibility is here: http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C++#The_Do.27s_and_Don.27ts
To answer your question, you can re-order non-virtual functions but not member variables or virtuals.
Strictly speaking, you have undefined behaviour unless both class definitions are identical (defined as consisting of the same sequence of tokens after preprocessing).
In practice, there probably won't be any issues as long as the memory layout of both classes remains the same. Adding, removing or reordering non-virtual member functions won't affect the layout. Doing that to base classes, data members or virtual member functions will affect the layout, and could lead to horrendously inscrutable bugs.
Since it's impossible to prevent incompatible changes, I'd strongly advise you not to do this. Either find a way to share the same class definition between projects, or fully branch it and maintain each branch separately.
Function declaration order matters especially if the function declaration has default arguments.
For example in the below function declaration if you change the declaration order then the compile gives you - missing default parameter error. Reason is compiler allows you to separate the function declaration with default argument with in the same scope but it should be in order from RIGHT to LEFT (default arguments) and from TOP to BOTTOM(order of function declaration default argument).
//declaration
void function(char const *msg, bool three, bool two, bool one = false);
//void function(char const *msg, bool three = true, bool two, bool one); //Error
void function(char const *msg, bool three, bool two = true, bool one); // OK
void function(char const *msg, bool three = true, bool two, bool one); // OK
int main() {
function("Using only one Default Argument", false, true);
function("Using Two Default Arguments", false);
function("Using Three Default Arguments");
return 0;
}
//definition
void function(char const *msg, bool three, bool two, bool one ) {
std::cout<<msg<<" "<<three<<" "<<two<<" "<<one<<std::endl;
}