Results 1 to 4 of 4

Thread: inheritence and virtual

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default inheritence and virtual

    Hello!

    Admittedly, I'm not the most experienced C++ programmer and this is the first time I'm trying to use inheritance and virtual functions. It doesn't work like it should and I can't figure out why. Here is the juice.

    Qt Code:
    1. class PathPiece
    2. {
    3. public:
    4.  
    5. virtual void pos()
    6. {
    7. qDebug() << "Called base";
    8. }
    9.  
    10. void draw(QPainter* painter)
    11. {
    12. pos();
    13. }
    14. };
    15.  
    16.  
    17. class LinePiece : public PathPiece
    18. {
    19. public:
    20.  
    21. void pos()
    22. {
    23. qDebug() << "Called line";
    24. }
    25. };
    To copy to clipboard, switch view to plain text mode 

    LinePiece is a kinf of PathPiece and it's supposed to override the pos() function, while the draw() function remains central in PathPiece. If I do

    Qt Code:
    1. LinePiece lp;
    2. lp.draw(painter);
    To copy to clipboard, switch view to plain text mode 

    Then everything works. However, if I do

    Qt Code:
    1. QList<PathPiece> list;
    2. list << LinePiece();
    3. list[0].draw();
    To copy to clipboard, switch view to plain text mode 

    then it doesn't. The pos() function of the base class is called. Isn't virtual supposed to be for cases exactly like this?

  2. #2
    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: inheritence and virtual

    You need to use pointers to achieve that, something like this:
    Qt Code:
    1. QList<PathPiece*> list;
    To copy to clipboard, switch view to plain text mode 
    you are truncating your values if you don't.

    So: Base obj = Derived objD; //isn't valid it performs a cast and you will have a Base object
    You need Base* obj = Derived *objD; // and this is ok, virtual mechanism works with this

  3. The following 2 users say thank you to Zlatomir for this useful post:

    Cruz (30th May 2010), zoz (30th May 2010)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: inheritence and virtual

    Thank you for the clarification!

  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: inheritence and virtual

    Your welcome, careful the last two line isn't c++ code, i said it in c++ style, just to make an example.

    And for your Base class write a destructor and declare it virtual (so the objects get destroyed properly no mater what object the Base pointer points to)

Similar Threads

  1. Virtual Keyboard
    By NoRulez in forum Qt Programming
    Replies: 9
    Last Post: 5th August 2010, 07:02
  2. QGraphicsItem inheritence
    By bajarangi in forum Newbie
    Replies: 4
    Last Post: 12th August 2009, 00:45
  3. Virtual Keyboard?
    By augusbas in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 14th July 2009, 08:55
  4. Virtual FB problem
    By nrabara in forum Newbie
    Replies: 0
    Last Post: 2nd October 2008, 06:05
  5. why triggerUpdate() is not virtual?
    By gadnio in forum Qt Programming
    Replies: 3
    Last Post: 12th January 2006, 15: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
  •  
Qt is a trademark of The Qt Company.