Results 1 to 5 of 5

Thread: C++ method call

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default C++ method call

    Hi,

    I have class structures like this:
    Qt Code:
    1. class classA
    2. {
    3. ...
    4. classA() //Constructor
    5. {
    6. foo();
    7. }
    8. virtual void foo()
    9. {
    10. //Do nothing
    11. }
    12. ...
    13. };
    14.  
    15. class classB : public classA
    16. {
    17. ...
    18. classB() : classA() //Constructor calls classA constructor
    19. {
    20. ...
    21. }
    22. void foo()
    23. {
    24. //Specific code here
    25. }
    26. ...
    27. };
    To copy to clipboard, switch view to plain text mode 

    When I create an object of classB, it calls the constructor from classA.
    The constructor of classA calls the method "foo()" that is redefined in the classB.
    The problem is that "foo()" is executed from classA instead of classB.

    Does anyone know how I must do this?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    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++ method call

    Since the constructor of classA is called before classB, the only chance you have is to create a init() method and call it from the classB constructor.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class classA
    4. {
    5. public:
    6. classA()
    7. {
    8. init(); // or don't call it if classA is abstract
    9. }
    10.  
    11. virtual void init()
    12. {
    13. qWarning() << Q_FUNC_INFO;
    14. foo();
    15. }
    16.  
    17. virtual void foo()
    18. {
    19. qWarning() << Q_FUNC_INFO;
    20. }
    21. };
    22.  
    23. class classB : public classA
    24. {
    25. public:
    26. classB() : classA()
    27. {
    28. init();
    29. }
    30. virtual void foo()
    31. {
    32. qWarning() << Q_FUNC_INFO;
    33. }
    34. };
    35.  
    36.  
    37. int main(int argc, char *argv[])
    38. {
    39. QApplication app(argc, argv);
    40. classB b;
    41. return 0;
    42. }
    To copy to clipboard, switch view to plain text mode 

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

    ^NyAw^ (18th November 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ method call

    Hi,

    Thanks Lykurg.

    This is the way that i did before but I was trying to not have to call "init()" from every derived class.

    Thank you anyway
    Òscar Llarch i Galán

  5. #4
    Join Date
    Feb 2008
    Posts
    2
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: C++ method call

    You can not call virtual functions from constructor.
    Such behavior is undefined - your class instance is not constructed yet.

    Regards,
    Alex

  6. #5
    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++ method call

    @Alex, it is not undefined behavior, only the "overriding" doesn't work.

    Overriding works only after the whole object is created because the objects are created from base class "down" - the base c-tor can't call a method in derived class since that slice of the object isn't created yet (or more correctly it's not yet initialized), so from c-tors and d-tor it's always the member function from the same class that gets called - so don't expect overriding.

  7. The following user says thank you to Zlatomir for this useful post:

    ^NyAw^ (21st November 2011)

Similar Threads

  1. None-GUI thread call widget method cause error
    By luochen601 in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2010, 05:39
  2. Replies: 6
    Last Post: 15th April 2010, 20:02
  3. Signal/slot or direct method call within a class
    By mike_the_tv in forum Newbie
    Replies: 6
    Last Post: 11th March 2010, 18:49
  4. Replies: 0
    Last Post: 20th December 2009, 15:37
  5. Why can't I call base public method?
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2009, 20:37

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.