Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the function declaration order matter in a header file?

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.

like image 811
dkg Avatar asked Nov 08 '13 12:11

dkg


People also ask

Does order matter in header files?

Ideally, all header files should be self-contained, and inclusion order should not matter. In practice, people often write header files that are not self-contained, and sometimes inclusion order does matter.

Does order of function declaration matter in C?

The order of functions inside a file is arbitrary. It does not matter if you put function one at the top of the file and function two at the bottom, or vice versa. Caveat: In order for one function to "see" (use) another function, the "prototype" of the function must be seen in the file before the usage.

Does declaration order matter in C++?

Yes, you must at least declare the class/function before you use/call it, even if the actual definition does not come until afterwards. That is why you often declare the classes/functions in header files, then #include them at the top of your cpp file.

Which function is declared in header file?

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.


1 Answers

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.

like image 125
Mike Seymour Avatar answered Sep 28 '22 02:09

Mike Seymour