H
H
haliverd2015-09-24 18:33:56
C++ / C#
haliverd, 2015-09-24 18:33:56

C++, function declarations and their memory layout, how are things going?

Good day. C++ language. Function descriptions follow:
1)

class test{
 void func(int x);
}
void test::func(int x){
%some_code%
}

2)
class test{
 func(int x){
 %some_code%
 }
}

The question is, is there a difference in how these functions are described, and if so, what is the difference? Will there be differences in their work? Is example 2 an example of what's best not to do in large projects?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-09-24
@Nipheris

Is example 2 an example of what's best not to do in large projects?

Most compilers will inline the function in this case.
The difference is very simple - in the first case you can move the implementation to a separate compilation unit, but in the second you can't. Why do this - read carefully about the compilation unit. Especially if you come from a language where there is no such task as separating include files and implementation files in principle.
Of course, there are cases when it is impossible to move the implementation into a separate cpp-shnik. The standard example is template classes. For them, the implementation of all functions will still have to be specified in the file that will be included. Therefore, there is no particular difference between 1 and 2 in this case.
Then read about inlining functions, this is probably what you heard.

V
Vladimir Martyanov, 2015-09-24
@vilgeforce

The disadvantage of the second option is that the declaration is inseparable from the implementation. As a result, Ad with inclusions, etc. IMHO.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question