Viswanathan V M
Viswanathan V M

Reputation: 11

Calling derived class function from base class function

In the following code, in the function set(int, int) of class Base, I want to call the derived class function showK(). Is there a way to do this?

I can not declare showK() function in class Base and i can not make that as virtual. This is a restriction for me.

class Base{
    int i, j;
    public:
    void set( int, int );
    void show() { cout << i << " " << j << "\n"; }
};

void Base:: set(int a, int b)
{ 
    i=a; j=b; 
    //Here I want to call the function showk() of class derived . Is there a way to call?.
}

class derived : public base {
    int k;
    public:
    derived(int x) { k=x; }
    virtual void showk() { cout << k << "\n"; }
};

int main()
{
    derived ob(3);
    ob.set(1, 2); // access member of base
    ob.show(); // access member of base
    ob.showk(); // uses member of derived class
    return 0;
}

Thanks in advance.

Upvotes: 0

Views: 3389

Answers (3)

Vaughn Cato
Vaughn Cato

Reputation: 64308

Calling a derived method when the object is not actually of that type could lead to undefined behavior. I think what you are actually wanting is to have the set() method call showk() if the object is of type derived. It would typically be done like this:

class Base{
    int i, j;
    public:
    virtual void set( int, int );
    void show() { cout << i << " " << j << "\n"; }
};

void Base:: set(int a, int b)
{ 
    i=a; j=b; 
}

class derived : public base {
    int k;
    public:
    derived(int x) { k=x; }
    virtual void set(int a,int b);
    virtual void showk() { cout << k << "\n"; }
};

void derived::set(int a,int b)
{
  base::set(a,b);
  showk();
}

int main()
{
    derived ob(3);
    ob.set(1, 2); // access member of base
    ob.show(); // access member of base
    ob.showk(); // uses member of derived class
    return 0;
}

Upvotes: 1

Andrew Tomazos
Andrew Tomazos

Reputation: 68638

In the function set(int, int) of class Base, I want to call the derived class function showK(). Is there a way to do this? I can not declare showK() function in class Base and i can not make that as virtual. This is a restriction for me

If you know this is an instance of class derived than you can cast this to derived and call the function...

void Base:: set(int a, int b)
{ 
    i=a; j=b; 
    ((derived* const) this)->showk();
}

You can always do a dynamic_cast instead, to test if it is a derived, if you want to do something different if it's not

(Having said that, I would suggest that if you are in this situation, than something may be wrong with your design. In general base classes shouldn't "know about" their derived classes.)

Upvotes: 1

planaria
planaria

Reputation: 101

template <class derived_type>
class Base{
    int i, j;
    public:
    void set( int, int );
    void show() { cout << i << " " << j << "\n"; }
};

template <class derived_type>
void Base<derived_type>:: set(int a, int b)
{ 
    i=a; j=b; 
    static_cast<derived_type*>(this)->showk();
}

class derived : public Base<derived> {
    int k;
    public:
    derived(int x) { k=x; }
    virtual void showk() { cout << k << "\n"; }
};

int main()
{
    derived ob(3);
    ob.set(1, 2); // access member of base
    ob.show(); // access member of base
    ob.showk(); // uses member of derived class
    return 0;
}

http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

Upvotes: 0

Related Questions