Results 1 to 8 of 8

Thread: virtual overloaded functions and base class function call...

  1. #1
    Join Date
    Feb 2006
    Posts
    26
    Thanked 2 Times in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default virtual overloaded functions and base class function call...

    Hello,


    I've got a comprehension problem of what happend in one of the project
    i'm working on. It's (I think) a pure c++ problem...

    Basically I've got a class gs_object than has got a VIRTUAL function
    createList(). This createList() function is overloaded in another class
    named ct_server that inherits gs_object.

    in my code, it looks something like that :

    Qt Code:
    1. class gs_object {
    2. ...
    3. virtual void createList();
    4. ...
    5. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class ct_server : public gs_object {
    2. ...
    3. virtual void createList();
    4. void initInstance();
    5. ...
    6. };
    To copy to clipboard, switch view to plain text mode 

    Here is the problem : in the function ct_server::initInstance, one boy of my team wanted to
    call the gs_object::createList() base function, and not the overloaded
    one (ct_server::createList() ). But, according to me he made a mistake
    as he wrote :

    (static_cast<GS_object*>(this))->createList();

    instead of

    gs_object::createList();

    According to me, as createList() is virtual, this line of code should
    call ct_server::createList and not gs_object::createList()

    But it doesn't : when in run in debug mode, i can see it calls
    gs_object::createList();

    I can't understand why. Could you explain me ?
    FYI, i'm using Visual C++ 7.1.3 ; Qt 3.3.4

  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: virtual overloaded functions and base class function call...

    Yes, this is a pure c++ "problem".

    Quote Originally Posted by nouknouk
    According to me, as createList() is virtual, this line of code should
    call ct_server::createList and not gs_object::createList()

    But it doesn't : when in run in debug mode, i can see it calls
    gs_object::createList();
    So which one you want it to call?
    With a scope operator "::" it was directed it to call base class implementation, not the overloaded one.

  3. #3
    Join Date
    Feb 2006
    Posts
    26
    Thanked 2 Times in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: virtual overloaded functions and base class function call...

    I wanted to call gs_object::createList, but that's not the problem in fact. With the syntaxt gs_object::createList(); it will work fine...

    My problem is that I don't understand why what I consider to be a mistake works althrough It shouldn't.

    (static_cast<GS_object*>(this))->createList();

    Calling this for me is equivalent to do something like :

    GS_object * myObj = (gs_object*)myCtServer;
    myObj->createList();

    but, as createList() is virtual, I should call ct_server::createList() even if the call is made from a gs_object pointer.

    So why does it works anyway ?

  4. #4
    Join Date
    Jan 2006
    Location
    England
    Posts
    18
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: virtual overloaded functions and base class function call...

    (static_cast<GS_object*>(this))->createList();

    This line will call the base class implementation because of the static_cast<GS_object*> call. static_cast will convert a pointer to a base class pointer.

    More info here...
    http://msdn2.microsoft.com/en-us/lib.../c36yw7x9.aspx

    HTH

    McToo
    It's always a long day, you can't fit 86400 into a short!

  5. #5
    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: virtual overloaded functions and base class function call...

    Quote Originally Posted by McToo
    This line will call the base class implementation because of the static_cast<GS_object*> call. static_cast will convert a pointer to a base class pointer.
    But that method is virtual, so cast shouldn't change anything.

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

    sh Code:
    1. $ ./a.out
    2. a.foo() : A::foo()
    3. b.foo() : B::foo()
    4. static_cast : B::foo()
    5. dynamic_cast : B::foo()
    6. b.bar() : A::foo()
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to jacek for this useful post:

    McToo (11th March 2006)

  7. #6
    Join Date
    Feb 2006
    Posts
    26
    Thanked 2 Times in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: virtual overloaded functions and base class function call...

    ok. I've got the solution :

    in fact, the declaration of my overloaded function in the derived class wasn't exactly the same as the base one (a parameter difference).

    That's why It was still calling the base function.

  8. #7
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: virtual overloaded functions and base class function call...

    There is a difference between Overloading and Overridding
    While you overload function in Derived class
    Take Care ...
    Have a look at this http://publib.boulder.ibm.com/infoce...se_derived.htm
    We can't solve problems by using the same kind of thinking we used when we created them

  9. The following user says thank you to sunil.thaha for this useful post:

    McToo (11th March 2006)

  10. #8
    Join Date
    Jan 2006
    Location
    England
    Posts
    18
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Red face Misconceptions...

    Nice one jacek & sunil!

    Thanks for the code post, ended a misconception that may have cost me some time in the future, although to call a base class function I wouldn't have used the static_cast call anyway. (Honest, guv!)

    McToo
    It's always a long day, you can't fit 86400 into a short!

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.