Solved

resolving overloaded function

Posted on 2002-06-17
10
241 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now