INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Calling methods that doesnt belong to the superclass

Calling methods that doesnt belong to the superclass

Calling methods that doesnt belong to the superclass

(OP)

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.
 

RE: Calling methods that doesnt belong to the superclass

It is something like

CODE

((SonB*)array[4])->exclusiveMethodSonB()

This is a static_cast if you like the extra typing.

RE: Calling methods that doesnt belong to the superclass

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;).
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close