Results 1 to 4 of 4

Thread: C++ inheritance

  1. #1
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default C++ inheritance

    I have read C++ guide line at google:
    http://google-styleguide.googlecode....ml#Inheritance

    following lines I can't understand, please help.

    All inheritance should be public. If you want to do private inheritance, you should be including an instance of the base class as a member instead

  2. #2
    Join Date
    May 2010
    Location
    Russia
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: C++ inheritance

    here is answer on your question:
    Use composition when you can, private inheritance when you have to.

    Normally you don't want to have access to the internals of too many other classes, and private inheritance gives you some of this extra power (and responsibility). But private inheritance isn't evil; it's just more expensive to maintain, since it increases the probability that someone will change something that will break your code.

  3. The following user says thank you to xray2000 for this useful post:

    yyiu002 (29th June 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: C++ inheritance

    It means you shouldn't do that:
    Qt Code:
    1. class foo : private bar {};
    To copy to clipboard, switch view to plain text mode 
    Instead do
    Qt Code:
    1. class foo {
    2. private:
    3. bar *pointerToBar;
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ inheritance

    Private inheritance is not recommended because it doesn't shape a "is-a" relationship (like OOP concepts say) private inheritance is more like composition, but it isn't that clear for others programmers, that read your code, what have you done in there (because most of us expect a "is-a" relationship between classes, when we see inheritance)

    And for two classes you probably think that it's not a "big" deal, but what do you do when you need to use let's say 5 other classes? (imagine the code that private inherit from 5 other classes) the compiler doesn't care, it's for other programmers from your team (and for you, because if you read the code in a couple of months, for some update/re-factoring you won't find the code easy to read)

    So this is a conceptual thing, it depends on what you want to model, if the new class (the one you are trying to create) will actually be a more specialized base class (the class that you use to build yours) you will use public inheritance (is-a relationship),
    and when you want the new class to just use the other (or others classes) use composition (has-a relationship)
    Last edited by Zlatomir; 29th June 2010 at 11:59.

Similar Threads

  1. Inheritance and QExplicitlySharedDataPointer
    By tbcpp in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2010, 08:18
  2. inheritance
    By steiner in forum Qt Programming
    Replies: 4
    Last Post: 30th October 2007, 20:17
  3. inheritance
    By mickey in forum General Programming
    Replies: 11
    Last Post: 28th September 2007, 21:54
  4. How much inheritance do you use?
    By Michiel in forum General Programming
    Replies: 8
    Last Post: 1st August 2006, 22:29
  5. QSA and inheritance
    By jwintz in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 14:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.