Results 1 to 4 of 4

Thread: Abstract base class and inherited class members

  1. #1
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Abstract base class and inherited class members

    I've got an abstract base with extremely minimal functionality, and several classes that inherit it. So for example...

    Qt Code:
    1. class AbstracBasetClass
    2. {
    3. };
    4.  
    5. class ClassA : public AbstracBasetClass
    6. {
    7. public:
    8. int numA;
    9. };
    10.  
    11. class ClassB : public AbstracBasetClass
    12. {
    13. public:
    14. int numB;
    15. };
    16.  
    17. class ClassC: public AbstracBasetClass
    18. {
    19. public:
    20. int numC;
    21. };
    To copy to clipboard, switch view to plain text mode 

    For the sake of simplicity each of the members is an int, but in my implementation they may be completely different and unrelated.

    Now in my code, I have to instantiate one of these inherited classes and access the members, then do something with the class. So, I'll have something like this...

    Qt Code:
    1. AbstracBasetClass* foo;
    2.  
    3. if (some condition)
    4. {
    5. foo = new ClassA();
    6. foo->numA = 1;
    7. }
    8. else if (some other condition)
    9. {
    10. foo = new ClassB();
    11. foo->numB = 2;
    12. }
    13. else if (some third condition)
    14. {
    15. foo = new ClassC();
    16. foo->numC = 3;
    17. }
    18.  
    19. // do something here with foo regardless of what its type is
    To copy to clipboard, switch view to plain text mode 

    Now this is perfectly valid of course, since foo is a pointer to the abstract base class and I'm merely instantiating one of the inherited classes. The problem is when I try to access the member, the compiler complains that "class AbstractBaseClass has no member named" and the member name in question. So instead, I have to do something like

    Qt Code:
    1. foo = new ClassA();
    2. ((ClassA*)foo)->numA = 1;
    To copy to clipboard, switch view to plain text mode 

    which works, but looks ugly and clunky.
    I could just declare foo within each if block, but then the part at the end where I "do something with foo" fails, because the compiler says that foo isn't defined.

    I'm wondering if there's any way around this, or is this it?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Abstract base class and inherited class members

    Why not have a design like this -
    Qt Code:
    1. class AbstracBasetClass
    2. {
    3. public:
    4. int number;
    5. };
    6.  
    7. class ClassA : public AbstracBasetClass
    8. {
    9. public:
    10. };
    11.  
    12. class ClassB : public AbstracBasetClass
    13. {
    14. public:
    15. };
    16.  
    17. class ClassC: public AbstracBasetClass
    18. {
    19. public:
    20. };
    To copy to clipboard, switch view to plain text mode 
    and then simply call foo->number = value;

    or you could also do something like -
    Qt Code:
    1. class AbstracBasetClass
    2. {
    3. public:
    4. virtual void setNumber(int number) {} // empty implementation in base class
    5. };
    6.  
    7. class ClassA : public AbstracBasetClass
    8. {
    9. public:
    10. int numA;
    11. void setNumber(int number) {numA = number; }
    12. };
    13.  
    14. class ClassB : public AbstracBasetClass
    15. {
    16. public:
    17. int numB;
    18. void setNumber(int number) {numB = number; }
    19. };
    20.  
    21. class ClassC: public AbstracBasetClass
    22. {
    23. public:
    24. int numC;
    25. void setNumber(int number) {numC = number; }
    26. };
    To copy to clipboard, switch view to plain text mode 

    then from your code you can call foo->setNumber(1);, etc depending on your if condition.

    Hope some way fits you

  3. #3
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Abstract base class and inherited class members

    Thanks for the reply aamer4yu, but as I said, the member is not necessarily the same for each inherited class. ClassA for example might have bools, ClassB might have ints and floats, ClassC might have a separate object, etc. So the members of the different classes have nothing in common.

  4. #4
    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: Abstract base class and inherited class members

    Quote Originally Posted by JovianGhost View Post
    which works, but looks ugly and clunky.
    But it is the "only" way. If don't want to use the casting all the time you can do it like that:
    Qt Code:
    1. AbstracBasetClass* foo;
    2.  
    3. if (some condition)
    4. {
    5. ClassA* classAPtr = new ClassA();
    6. classAPtr->numA = 1;
    7. foo = classAPtr;
    8. }
    9.  
    10. foo->whatEver();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. for i = 0 to nr members in a class ...
    By qt_gotcha in forum Qt Programming
    Replies: 4
    Last Post: 6th March 2010, 19:38
  2. Replies: 3
    Last Post: 27th December 2008, 19:34
  3. MVC - Abstract Widget Base Class - setupUI
    By SenSej in forum Newbie
    Replies: 0
    Last Post: 13th October 2008, 10:44
  4. Replies: 4
    Last Post: 19th March 2008, 17:47
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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.