-1

Reading about encapsulation in Lippman book, I found the following pargraph

Although user code need not change when a class definition changes, the source files that use a class must be recompiled any time the class changes.

Assuming I have the following code

class A {
private:
    int x;

public:
    A() { x = 0; }

    int x_plus();
}

int A::x_plus(){ return x + 1; }

if I changed the body of x_plus() to return x+2 , why I shall recompile source file that use it ?

I think it's something related to linking process by C++ , am I right ?

user4581301
  • 33,082
  • 7
  • 33
  • 54
Shady Atef
  • 2,121
  • 1
  • 22
  • 40
  • Your question doesn't quite make sense. You need to recompile the class definition (along with all the code that changed). All of that is both obvious and automatic. – xyious Nov 03 '17 at 21:20
  • @xyious, I am asking about the user code that will use my class. not class's source code. – Shady Atef Nov 03 '17 at 21:25
  • C++ build tools use very simple dependency checking, they only look at the timestamp of the source file. So if you would *not* use inline function definitions, keep the declaration in a .h file and the implementation in a .cpp file, then such a modification won't cause other source files to get rebuilt. – Hans Passant Nov 03 '17 at 21:27

1 Answers1

3

When you put the function definition inside the class definition, it's treated as an inline function by default. This means that any caller could have the body of the function copied into it. If you change the definition, the callers will still contain the old definition unless you recompile them.

See Function declaration inside or outside the class

If you change a function whose definition is not in the header file for the class, you don't need to recompile the callers, you just need to relink them. This is not considered a change to the class definition, so the quote from the Lippman book doesn't apply.

A simpler way to look at this is that the class definition should be in a .h file, while the code for the class will be in a .cpp file, which gets compiled into a .o file. If you make any changes to the .h file, you need to recompile anything that contains <include filename.h>. If you make any changes to the .cpp file, you'll recompile the .o file and relink the executables that use it. Application build tools (like make on Unix) should handle this automatically for you.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • And if you're using LTO this can still happen even when not defined inline ;-) – Nikos C. Nov 03 '17 at 21:23
  • I added the example myself, so I think it changed the meaning of the example. assuming another situation with no inline functions, (Changing the implementation of functions defined outside the class) – Shady Atef Nov 03 '17 at 21:24
  • If you change the implementation of non-inline functions outside the class definition, you don't need to recompile the callers, you just need to re-link them to the `.o` file for the class. – Barmar Nov 03 '17 at 21:26
  • That's what I know, but I am bit confused by the statement in Lippman book – Shady Atef Nov 03 '17 at 21:28
  • The book says "class definition". If the function definition isn't inside the class, it's not a change to the class definition. – Barmar Nov 03 '17 at 21:29