Results 1 to 4 of 4

Thread: Abstract method inheritance

  1. #1
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Abstract method inheritance

    I have two classes of the form:
    Qt Code:
    1. class DNumber
    2. {
    3. static int Digits;
    4. virtual DNumber* add(const DNumber* a, unsigned digits) const = 0;
    5. DNumber* add(const DNumber* a) const {return add(a, Digits);}
    6. }
    7.  
    8. class DInteger : public DNumber
    9. {
    10. // implementation uses runtime type checking to perform addition
    11. DInteger(int value);
    12. DNumber* add(const DNumber* a, unsigned digits) const;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Attempting to use this:
    Qt Code:
    1. DInteger* cien = new DInteger(100);
    2. DInteger* dos = cien->add(cien);
    3. qDebug() << dos->toString();
    4.  
    5. delete cien, delete dos;
    To copy to clipboard, switch view to plain text mode 

    Results in a compile error:

    error: no matching function for call to 'DInteger::add(DInteger*&)'

    DInteger should inherit add(DNumber*) right?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Abstract method inheritance

    error: no matching function for call to 'DInteger::add(DInteger*&)'
    Somewhere in your code you're adding a value and not a pointer.

  3. #3
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Abstract method inheritance

    The error is from line 2 of the second box of code. cien is a pointer. Also, code completion thinks that add(DNumber*) does not exist for DInteger as well. But it inherits DNumber! Why? It must be the call to a pure virtual function--other methods are inherited properly. Is there any way to make this work?

    Qt Code:
    1. #include <iostream>
    2.  
    3. int Digits = 20;
    4.  
    5. class A
    6. {
    7. virtual A* f(const A* a, int b) const = 0;
    8. A* f(const A* a) const {return f(a, Digits);}
    9. };
    10.  
    11. class B : public A
    12. {
    13. A* f(const A* a, int b) const
    14. {
    15. std::cout << b;
    16. return new B();
    17. }
    18. };
    19.  
    20. int main(int argc, char ** argv)
    21. {
    22. B* foo= new B();
    23. B* bar= new B();
    24. B* r = foo->f(bar);
    25.  
    26. delete foo, delete bar, delete r;
    27.  
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 
    Same error. no match for B::f(B*&)
    Last edited by space_otter; 31st October 2010 at 18:23.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Abstract method inheritance

    You can not overload the function f in this way. All the A::f functions get hidden by the B::f function.
    Either implement all the A::f functions in B too or rename A::f to A::f2

    Example:

    Qt Code:
    1. A* f2(const A* a) const {return f(a, Digits);}
    To copy to clipboard, switch view to plain text mode 

    Then do something like this:
    Qt Code:
    1. A* r = foo->f2(bar);
    To copy to clipboard, switch view to plain text mode 

    Edit:
    Be careful with your class definitions.
    Your functions are all private and you do not define any constructors. Note that the compiler will fill these in automatically, but it might not be what you want.
    Last edited by tbscope; 31st October 2010 at 19:28.

Similar Threads

  1. Replies: 2
    Last Post: 1st September 2010, 08:21
  2. Abstract threading
    By TheJim01 in forum Newbie
    Replies: 15
    Last Post: 14th April 2010, 21:46
  3. Abstract signals?
    By TorAn in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2010, 19:31
  4. Quick question regarding abstract classes and DLLs
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2007, 21:32
  5. abstract scroll area
    By moowy in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2006, 09:15

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.