Is ordering suitable for class methods for the compiler?

I am using a third-party static library (.lib file) in a C ++ project. The author of the static library added me a method for the class and sent me an updated library assembly.

Unfortunately, he did not send a new header file, and communication with him is slow, so I may not receive a new header in the near future. I know the method signature of the new method, so I can just add it to the header file.

My question is whether it matters when in the list of public methods I add a new declaration (top, bottom, middle ...). My best guess is what it does and that the order in the header file determines the order in the compiled class. Can someone confirm or deny this?

+5
source share
3 answers

My question is whether it matters when in the list of public methods I add a new declaration

AFAIK, it doesn't matter if a member function is a regular function, but it matters if it is a virtual member function. The virtual member functions in the virtual table are in a specific order. If the library has them in a different order than your .h file, most likely you will call the wrong function.

Related: Set the order of functions in a table of virtual methods?

+11
source

If the method declaration does not use types declared in the class, for example, the type of the return value, then it does not matter where the method is declared.

Otherwise, if the declaration of the method depends on declarations of other types within the class, then an error may occur.

For example, the compiler will throw an error for this class definition

 struct A { B f(); struct B {}; //... }; 

because type struct B used in the declaration of the member function f until the declaration of the structure.

0
source

Method declarations in a class should not matter to the compiler, unless it depends on a variable declaration that has not yet been executed.

0
source

Source: https://habr.com/ru/post/1241429/


All Articles