D
Dave
Hello all,
Please consider this code:
#ifndef FOO_INCLUDED
#define FOO_INCLUDED
// File: foo.h
class foo
{
public:
// Defined here in the header, implicitly inline
void func_1() {}
void func_2();
};
// Defined here in the header, not implicitly inline
void foo::func_2() {}
#endif
If this file gets included by more than one .cpp file, a link error along
the lines of "multiply defined synbol" will result because of foo::func_2().
Now, let's take foo::func_2() out, leaving only foo::func_1(). There is no
longer an error. This shows that foo::func_1() does not induce this error;
this is due to the fact that it is (implicitly) inline. There is presumably
no foo::func_1() symbol generated since a call to it is never generated. In
lieu of a call, its contents are simply plopped in place.
Now, this raises a question. A compiler is not required to honor an inline
request, even an implicit one. So, it would seem that defining a member
function inside of your class declaration would be very risky. If the
compiler chooses to not make the function inline, your going to get a
"multiply defined symbol" error. Yet, I have never seen this happen, and I
know that it is very common practice to define one or two line member
functions inside of their class' definition. I see this done by experts in
modern C++ literature all the time.
So, what am I missing here? How is it that the way I have declared / defined
foo::func_1() above is not putting me in jeopardy of a "multiply defined
symbol" linker error?
Thanks,
Dave
Please consider this code:
#ifndef FOO_INCLUDED
#define FOO_INCLUDED
// File: foo.h
class foo
{
public:
// Defined here in the header, implicitly inline
void func_1() {}
void func_2();
};
// Defined here in the header, not implicitly inline
void foo::func_2() {}
#endif
If this file gets included by more than one .cpp file, a link error along
the lines of "multiply defined synbol" will result because of foo::func_2().
Now, let's take foo::func_2() out, leaving only foo::func_1(). There is no
longer an error. This shows that foo::func_1() does not induce this error;
this is due to the fact that it is (implicitly) inline. There is presumably
no foo::func_1() symbol generated since a call to it is never generated. In
lieu of a call, its contents are simply plopped in place.
Now, this raises a question. A compiler is not required to honor an inline
request, even an implicit one. So, it would seem that defining a member
function inside of your class declaration would be very risky. If the
compiler chooses to not make the function inline, your going to get a
"multiply defined symbol" error. Yet, I have never seen this happen, and I
know that it is very common practice to define one or two line member
functions inside of their class' definition. I see this done by experts in
modern C++ literature all the time.
So, what am I missing here? How is it that the way I have declared / defined
foo::func_1() above is not putting me in jeopardy of a "multiply defined
symbol" linker error?
Thanks,
Dave