Thread: Inheritance in Pimpl idiom

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Inheritance in Pimpl idiom

    Hello everyone,


    Why it is useless for any inheritance in Pimpl idiom? Here is the source, but no detailed reasons are provided.

    http://www.gotw.ca/gotw/024.htm

    --------------------
    - make XImpl entirely the class that X would have been, and write X as only the public interface made up entirely of simple forwarding functions (a handle/body variant).

    This is useful in a few restricted cases, and has the benefit of avoiding a back pointer since all services are available in the pimpl class. The chief drawback is that it normally makes the visible class useless for any inheritance, as either a base or a derived class.
    --------------------


    thanks in advance,
    George

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by George2 View Post
    Hello everyone,


    Why it is useless for any inheritance in Pimpl idiom? Here is the source, but no detailed reasons are provided.
    Because inheritance is meant to leverage another class's implementation. But Pimpl is meant to do the opposite -- hide the implementation. You can inherit from the Pimpl interface but you are stuck making explicit calls to the underlying implementation -- you can't take advantage of how inheritance normally works where all the parent class's methods are "just there."

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks brewbuck,


    I do not quite understand your below comments. Could you provide more description please or some pseudo code? Especially what do you mean "take advantage of how inheritance normally works" and parent class's methods are "just there."?

    Quote Originally Posted by brewbuck View Post
    but you are stuck making explicit calls to the underlying implementation -- you can't take advantage of how inheritance normally works where all the parent class's methods are "just there."

    regards,
    George

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by George2 View Post
    Thanks brewbuck,


    I do not quite understand your below comments. Could you provide more description please or some pseudo code? Especially what do you mean "take advantage of how inheritance normally works" and parent class's methods are "just there."?
    Say you had some underlying class which provides functionality X() and Y(). Suppose the class looks like this:

    Code:
    class ClassGuts
    {
    public:
        someThing();
        someThingElse();
    };
    So the job of the Pimpl wrapper is to implement X() and Y() in terms of someThing() and someThingElse(). The two some*() functions are implementation details. The user will only see X() and Y(). So suppose you inherit from the wrapper. You can still call X() and Y() but you can't access the underlying ClassGuts object because that's the whole point of having a wrapper. You could inherit from ClassGuts directly, but now you've rendered the Pimpl idiom irrelevant.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks brewbuck,


    But invoking X() and Y() directly (other than inheriting from ClassGuts directly) will have any limitations? We can achieve some*() functions through X() and Y().

    Any disadvantages?

    Quote Originally Posted by brewbuck View Post
    Say you had some underlying class which provides functionality X() and Y(). Suppose the class looks like this:

    Code:
    class ClassGuts
    {
    public:
        someThing();
        someThingElse();
    };
    So the job of the Pimpl wrapper is to implement X() and Y() in terms of someThing() and someThingElse(). The two some*() functions are implementation details. The user will only see X() and Y(). So suppose you inherit from the wrapper. You can still call X() and Y() but you can't access the underlying ClassGuts object because that's the whole point of having a wrapper. You could inherit from ClassGuts directly, but now you've rendered the Pimpl idiom irrelevant.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Pimpl Idiom client/serve compile/link
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2008, 06:25 AM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM