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?