Results 1 to 3 of 3

Thread: diffrence between void and virtual void

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default diffrence between void and virtual void

    Hello everybody,

    can somebody say me when i have to use virtual void?And the difference between virtual void and void?


    thx
    Think DigitalGasoline

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: diffrence between void and virtual void

    The virtual keyword defines that a class function may be overridden in a derived class.
    So the derived class may have an implementation of a function with same name and argument list but with different/extended functionality.

    Edit: http://www.cppreference.com/keywords/virtual.html
    Last edited by jpn; 23rd February 2006 at 14:55.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: diffrence between void and virtual void

    virtual keyword has nothing to do with return type of a method. It only tells that the following method is virtual (you can skip virtual in subclasses).

    Try this:
    Qt Code:
    1. #include <iostream>
    2.  
    3. class A
    4. {
    5. public:
    6. A() {}
    7. void nonVirt() { std::cerr << "A::nonVirt()" << std::endl; }
    8. virtual void virt() { std::cerr << "A::virt()" << std::endl; }
    9. virtual ~A() {}
    10. };
    11.  
    12. class B : public A
    13. {
    14. public:
    15. void nonVirt() { std::cerr << "B::nonVirt()" << std::endl; }
    16. void virt() { std::cerr << "B::virt()" << std::endl; }
    17. };
    18.  
    19. int main()
    20. {
    21. A a;
    22. B b;
    23. A *ptr = &b;
    24.  
    25. a.nonVirt();
    26. a.virt();
    27. b.nonVirt();
    28. b.virt();
    29. ptr->nonVirt();
    30. ptr->virt();
    31.  
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Q3ScrollView resists to scroll down to the garbage bin
    By sivrisinek in forum Qt Programming
    Replies: 0
    Last Post: 5th February 2009, 18:50
  2. Problems with QDate
    By cyberboy in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2008, 22:17
  3. Delayed Rendering of QTabWidget Tabs
    By mclark in forum Qt Tools
    Replies: 13
    Last Post: 14th May 2007, 23:53
  4. Compiling error
    By vfernandez in forum Newbie
    Replies: 2
    Last Post: 9th March 2007, 22:02
  5. Problems with Q_OBJECT and subclassing
    By renaissanz in forum Qt Programming
    Replies: 4
    Last Post: 21st February 2006, 23:18

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.