Thread: pimpl confuses me

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    pimpl confuses me

    Code:
    class Hat
    {
    	struct Cat;
    	Cat * cat;
    public:
    	Hat() { cat = new Cat; }
    	~Hat() { delete cat; }
    	void printCat() { cout << cat->name << endl; }
    };
    
    
    // version 1
    class Cat
    {
    public:
    	string name;
    	Cat() { name = "Lucy"; }
    };
    
    // version 2
    class Cat
    {
    public:
    	string color;
    	string name;
    	Cat() { color = "Brown"; name = "Lucy"; }
    };
    
    int main()
    {
    	Hat hat;
    	hat.printCat();
    	return 0;
    }
    The Pimpl is a little confusing for me.
    Here in my example Hat has a Cat* so that it will all work fine even when Cat gets changed. So called, binary compatibility.
    Now if I compiled my program using version 1 but now I have access to version 2 .dll then. Will it not fail? Why it will not fail?

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    actually what confuses me even more is that no one could reply to this. that makes me think that
    pimple confuses all the people. lol haha lol

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why don't you test first?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to get a working example of the pimpl before you can test it. Your code doesn't work.

    Also, have you thought about the fact that the code that uses the Cat must see its definition? So if the definition changes the code that uses it will have to be recompiled as well.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    laserlight and Daved sorry, but the example was not meant to be compiled and run. it is just a
    little modification of the original example given at wiki.
    i just wanted to hint you at what i am talking about. now if i had compiled my program with version 1 in a dll. and then got the version 2 dll. will my program still run??

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I'm not convinced you understand what pimpl is all about. My understanding is that it is for speeding up compile times. In your example, Hat will always need to be recompiled when Cat changes. It is the callers of Hat that do not need to be recompiled. And that is where you get your savings. The following articles might be helpful...

    http://www.gotw.ca/publications/mill05.htm
    http://www.gotw.ca/gotw/028.htm
    http://www.devx.com/cplus/Article/28105

    Also your talk about binary compatibility and DLL's has more to do with COM than pimpl.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    well may be i am confusing a lot of things. you know whenever i start programming in c++ i begin thinking about all such stuff.
    and my point of focus moves from the logic of my program, to the implementation details, memory management etc.

    i am so very confused with c++ these days.

    just as i am writing this post, i was confused about my constructor also
    should it be
    Classname(BigObject & ref)
    OR
    Classname(BigObject obj)

    i was so much confused that i stopped doing it and started watching movie

  8. #8
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You should definitely spend the time to learn the basics before diving into pimpl idiom.

    Your constructor should pass by reference also if you're passing a big object. If you do not intend to modify the BigObject, then you should pass by const refererence, and if you DO intend to modify BigObject, then you pass by reference.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks IfYouSaySo!
    Whats your real name, the ID sounds very strange, see I wanted to say thanks, but, if read carefully it means, thanks if you say so
    Real name please!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance in Pimpl idiom
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 09:32 PM
  2. Pimpl Idiom client/serve compile/link
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2008, 06:25 AM