Link to home
Start Free TrialLog in
Avatar of IainHere
IainHere

asked on

resolving overloaded function

class base;

class derivedA : public base //etc
class derivedB : public base //etc
...
base* basePointerA = new derivedA;
base* basePointerB = new derivedB;
...
void aFunction(derivedA*); //how to call this?
void aFunction(derivedB*);

I have an array of base*, which actually point to derived objects.  I also have some functions that expect pointers to derived objects.  Is there any way I can resolve the function to use based on the basePointerA and basePointerB in the example above?

I was hoping that simply calling
aFunction(basePointerA);
would put me into the body of aFunction(derivedA*), but this isn't the case.  I'm on the verge of putting a virtual int getType() = 0;
function into the base class - what are my options?

Thanks for your help (+ apologies for wording the question awkwardly)
Avatar of MDarling
MDarling

Is aFunction virtual in the base class - if so, it should resolve correctly to the derived classes.

class Base
{
 virtual void aFunction();
};

class DerivedA : public base
{
 void aFunction();
}

class DerivedB : public base
{
 void aFunction();
}

main()
{
  Base* p=new DerivedA();
  Base* q=new DerivedB();

  p->aFunction(); // calls DerivedA::aFunction();
  q->aFunction(); // calls DerivedB::aFunction();
 
}




Fuller example...




#include <iostream>

using namespace std;


class Base
{
public:
    Base() {}
    virtual void aFunction() { cout << "Base\n";}
};

class DerivedA : public Base
{
public:
    DerivedA() {}
    void aFunction()
        {
            cout << "DerivedA\n";
        }
};

class DerivedB : public Base
{
public:
    DerivedB() {}
    void aFunction()
        {
            cout << "DerivedB\n";
        }
};


int main()
{
    Base* p=new DerivedA();
    Base* q=new DerivedB();
    p->aFunction(); // calls DerivedA::aFunction();
    q->aFunction(); // calls DerivedB::aFunction();

    return 0;
}
Avatar of IainHere

ASKER

No, aFunction is not a member of the class.  In fact, what it does is adds the information stored in the derived classes to a tree display of the info.  So it is a member function of a class called CProjectTree, but for the purposes of this example, it might as well be global.

Thanks for the feedback anyway.
ASKER CERTIFIED SOLUTION
Avatar of MDarling
MDarling

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Axter
Just to add to what MDarling said,
I think the virtual function-calling Global function method is the best way to go.

Here's a working example:
#include <stdlib.h>
#include <iostream>
using namespace std;

class base
{
public:
     virtual ~base(){}
     virtual void aFunction()=0;
};

class derivedA : public base
{
public:
     void SomeFunction(){cout << "derivedA" << endl;}
     virtual void aFunction();
};

class derivedB : public base
{
public:
     void SomeFunction(){cout << "derivedB" << endl;}
     virtual void aFunction();
};

void aFunction(derivedA* a)
{
     a->SomeFunction();
}
void aFunction(derivedB* b)
{
     b->SomeFunction();
}

void derivedA::aFunction()
{
     ::aFunction(this);
}

void derivedB::aFunction()
{
     ::aFunction(this);
}

int main(int argc, char* argv[])
{
     base* basePointerA = new derivedA;
     base* basePointerB = new derivedB;

     basePointerA->aFunction();
     basePointerB->aFunction();

     delete basePointerA;
     delete basePointerB;
     system("pause");
     return 0;
}
Firstly I am assuming that aFunction is in global scope ..

you have

base* basePointerA = new derivedA;
base* basePointerB = new derivedB;

and overloaded functions
void aFunction(derivedA*); //how to call this?
void aFunction(derivedB*);

so what you to is this ..

aFunction( (derivedA*)basePointerA ); // c-style cast ..
 
... this will ensure the right one is called ..





Hi IainHere,

You can do as it is, the only thing is that you have to type cast the base class pointer variable to derivedA or derivedB pointer. It will work perfectly, but make sure that the base class pointer variable is pointing to the derived class object.

Just look at the following sample.#include <iostream>
using namespace std;

class base
{
        public :
        void show()
        {
                cout << "base show()" << endl;
        }
};

class derivedA : public base
{
        public :
        void show()
        {
                cout << "derievedA show()" << endl;
        }
};

class derivedB : public base
{
        public :
        void show()
        {
                cout << "derivedB show()" << endl;
        }
};

void baseFunction(base *obj)

void aFunction(derivedA *obj)
{
        obj->show();
}

void bFunction(derivedB *obj)
{
        obj->show();
}

int main()
{
        base *basePointerA = new derivedA;
        base *basePointerB = new derivedB;
        //Base class show
        baseFunction(basePointerA);
        baseFunction(basePointerB);

        //Derived class show
        aFunction( (derivedA *) basePointerA);
        bFunction( (derivedB *) basePointerB);

        return 0;
}

In addition to my pervious post, you can change the bFunction name into aFunction [ Overloading ] and the bFunction( (derivedB *) basePointerB) function call into aFunction( (derivedB *) baePointer B)
Accepted this answer yesterday - musn't have registered.

Anyway, thanks for the help.  Axter - points for your useful example forthcoming.