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?