Jump to content

Pimpl-idiom and compilation dependencies

- - - - -

  • Please log in to reply
6 replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hei,

in his book Effective C++, Scott Meyers claims that including header files creates a dependency between the two entities. Furthermore, he claims that if main depends on Employee.h and Employee.h depends on Date.h, the dependency works all the way through the dependencies in such a way that if Date changes, all will have to recompile.

A solution for this kind of situation is the pimpl-idiom (pointer to implementation). The Employee class doesn't have a Date-object, it has a pointer (smart or otherwise) to EmployeeImpl-class object. The EmployeeImpl carries all of the implementation details and Employee-class objects merely forward methods calls to the implementation-class' methods.

This is all clear to me. My problem right now is that I suspect this isn't true. There is no need for recompilation if Date is changed. I created a simple test and the result was that Employee-object never needed recompilation and in fact, neither did main.o. Only Date-class was recompiled after it was changed and after that the three object-files were linked. This is exactly what was suppose to happen were I to have used the pimpl-idiom.

Has Scott Meyers made a mistake? Does GCC do things differently? Jupiter facing Mars again :) ?

PS. The source used for this test is included in its original form without the modifications made to Date-class.

Attached Files



#2
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Almost forgot the book details:
Effective C++, 3rd edition(2005) by Scott Meyers
Chapter 5, Item 31 (p. 140-148): Minimize compilation dependencies between files.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Currently, you have a trivially simple Date class. Try adding a few methods, such as GetMonth() and GetYear() to it, then change the implementation of those and see what happens.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

WingedPanther said:

Currently, you have a trivially simple Date class. Try adding a few methods, such as GetMonth() and GetYear() to it, then change the implementation of those and see what happens.

Already tried adding getYear, no change. Sorry, should've mention that.
But simplicity is the key then ?

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I'll try to come up with an example this evening when I get home.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
This is because GNU make doesn't necessarily know what files your .cc files are dependent on. If you try compiling after changing a dependency, you'll notice your make will not build changes in header files at all, even ones that should work and will result in bugs! This is because of make's design, in an attempt to compile what you need and ONLY what you need. In order to ensure that make builds only what you want it to, you need to list the file's dependencies in your Makefile.

Try this Makefile.
CC=g++

CFLAGS=-g -Wall

CPPFLAGS=$(CFLAGS)

OBJECTS=Employee.o main.o Date.o


default: main


main: $(OBJECTS)

	$(CC) $(CFLAGS) -o $@ $^


$(OBJECTS) : Date.h

Employee.o main.o : Employee.h


clean:

	rm -f $(OBJECTS) main

Wow I changed my sig!

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Also try using template classes, which have to be implemented in the header file they're declared in. That'll definitely break your code if you don't recompile.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users