?
Solved

resolving overloaded function

Posted on 2002-06-17
10
Medium Priority
?
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
MDarling earned 400 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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

719 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