Solved

resolving overloaded function

Posted on 2002-06-17
10
254 Views
Last Modified: 2010-04-01
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)
0
Comment
Question by:IainHere
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:MDarling
ID: 7083710
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();
 
}




0
 
LVL 3

Expert Comment

by:MDarling
ID: 7083719
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;
}
0
 
LVL 4

Author Comment

by:IainHere
ID: 7083747
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.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 3

Accepted Solution

by:
MDarling earned 100 total points
ID: 7083776
I see (I think).

I suppose you could always add a virtual function to the base class and overload it in the derived classes and then call the Global [sic] function from those with a this pointer as a param.  Doesn't sound very nice though.

Other than that you could use RTTI, or as you said implement your own.



0
 
LVL 30

Expert Comment

by:Axter
ID: 7083923
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;
}
0
 

Expert Comment

by:balugaa
ID: 7083950
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 ..





0
 

Expert Comment

by:smaniraja
ID: 7086260
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;
}

0
 

Expert Comment

by:smaniraja
ID: 7086409
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)
0
 
LVL 4

Author Comment

by:IainHere
ID: 7086417
Accepted this answer yesterday - musn't have registered.

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

Author Comment

by:IainHere
ID: 7086426
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question