Thread: Compilation dependency and the pimpl idiom

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37

    Compilation dependency and the pimpl idiom

    So, I just read through Item 31 in Effective C++ and it confused the hell out of me. The idea is to minimize compilation on the client's part and I get why it's desirable, but I just don't see how it's possible. In order for code to run it needs to be compiled, right? And therefore if you change anything pertaining to it, it needs to be compiled again. There's no way the changes would show up unless they are made at runtime. One part that confused me in particular was his use of the pimpl idiom to separate the interface and the implementation:

    Code:
    #include <string>
    
    #include <memory>
    
    class PersonImpl;
    
    class Date;
    class Address;
    
    class Person {
    public:
       Person(const std::string& name, const Date& birthday, const Address& addr);
       std::string name() const;
       std::string birthDate() const;
       std::string address() const;
       …
    
    private:
       std::tr1::shared_ptr<PersonImpl> pImpl;
    };
    And this is the explanation that follows:

    "Here, the main class contains as a data member nothing but a pointer to its implementation class.

    With this design, clients of Person are divorced from the details of dates, addresses, and persons. The implementations of those classes can be modified at will, but Person clients need not recompile."

    I don't understand that. Why would they not need to recompile if the implementation has been changed? If he is talking about changes at runtime, can't you also modify the data members through member functions at runtime with no need to compile? I really have no idea what he's talking about.
    Last edited by ex-mortis; 06-30-2012 at 12:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding forward declarations of classes, pimpl
    By Boxknife in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2010, 01:29 AM
  2. Identifying a C++ idiom
    By kovacsbv in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2009, 02:50 PM
  3. pimpl confuses me
    By manav in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2008, 10:31 PM
  4. Inheritance in Pimpl idiom
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 09:32 PM
  5. Pimpl Idiom client/serve compile/link
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2008, 06:25 AM