Solved

resolving overloaded function

Posted on 2002-06-17
10
259 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

837 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