Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling methods that doesnt belong to the superclass

Status
Not open for further replies.

abcd12344445555

Programmer
May 10, 2009
24
BR
Code:
class Parent
{
 public:
    virtual void functionA() = 0;
    virtual void functionB() = 0;
};


class SonA : public Parent
{
public:
//implements virtual methods
   void functionA(); //...
   void functionB(); //...


//SonA's exclusive method
   void exclusiveMethodSonA();
};

class SonB : public Parent
{
public:
//implements virtual methods
   void functionA(); //...
   void functionB(); //...

//SonB's exclusive method
   void exclusiveMethodSonB();
};


I have an array of Parent* and at any given position I can instantiate either a SonA or a SonB object:

EXAMPLE:
Code:
int main(int argc, char *argv[])
{
   Parent* array[10];
   array[0] = new SonA();
   array[1] = new SonB();
   array[2] = new SonA();
   array[3] = new SonA();
   array[4] = new SonB();
   //...
}

Given the scenario above, how can I access SonB's exclusive method? Something like: array[4]->exclusiveMethodSonB() wont work.
Do I have to create an interface with virtual methods in the Parent class, even for methods that wont be implemented in SonB and vice-versa?

Thanks.
 
It is something like
Code:
((SonB*)array[4])->exclusiveMethodSonB()

This is a static_cast if you like the extra typing.
 
Add virtual destructor to the Parent then use RTTI:
Code:
class Parent{ 
public:
    virtual ~Parent() {}
    virtual void functionA() = 0;
    virtual void functionB() = 0;
};
class SonA : public Parent
{
public:
//implements virtual methods   
    void functionA() { cout << "SonA::A()\n"; } //...
    void functionB() { cout << "SonA::B()\n"; } //...
  //SonA's exclusive method
    void exclusiveMethodSonA() { cout << "SonA::X()\n"; }
};
class SonB : public Parent
{
public:
//implements virtual methods   
  void functionA() { cout << "SonB::A()\n"; } //...   
  void functionB() { cout << "SonB::B()\n"; } //...
  //SonB's exclusive method   
  void exclusiveMethodSonB() { cout << "SonB::X()\n"; }
};

static void family()
{
    Parent* a[2];
    a[0] = new SonA;
    a[1] = new SonB;
    for (size_t i = 0; i < sizeof a/sizeof*a; ++i) {
        cout << i << ':';
        if (SonA* pa = dynamic_cast<SonA*>(a[i])) {
            pa->exclusiveMethodSonA();
        } else if (SonB* pb = dynamic_cast<SonB*>(a[i])) {
            pb->exclusiveMethodSonB();
        } else {
            cout << endl;
        }
    }
    delete a[0];
    delete a[1];
}
Don't forget to switch on RTTI support of your compiler.
It's legal but unpleasant code (forbidden in my project command;).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top