Link to home
Start Free TrialLog in
Avatar of IssacJones
IssacJones

asked on

Array of functions in a derived class

Hi

I have an application where I have a class CMyView.

In this class, I have created an array for functions e.g.

      typedef void (CMyView::*m_f)(CDC* pDC);
      CArray <m_f, const m_f&> m_farray;

This works fine, because I can do things like

      m_farray.Add( &CMyView::Function1 );

However, I have a problem. What I would like to do is to derive a new class from CMyView e.g. CMySecondView. From this, I want to add functions from this derived class and place them in the same array so that the base class can perform operations on them e.g. I want to do

      m_farray.Add( &CMySecondView::Function9 );

I run into compilation problems if I try to do this.

Can anybody see a way I can do this?

That is, I want the array of functions which are dealt with in the base class to be able to store functions from the dervived class.

John
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

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
note, the mfc casting of function pointers only works cause they forbid multiple inheritage. the pointers of the derived class instance must be the same as the baseclass pointer.

Sara
Avatar of IssacJones
IssacJones

ASKER

Hi Sara

I'm afraid you've lost me with a lot of your comments. I'm only really a newbie to c++/mfc.

I have come with a solution but it is not ideal. Maybe you can help further:

If I declare Function1 as virtual in the base class and then re-define it in the child class, I can do the following in the child class:

m_farray.Add( &CMyView::Function1 );

This works fine i.e. it calls Function1 in the child class when the function array element is called.

My problem is this, for this to work I have to declare all the functions I will be using in the base class and the child class and then override in the child class.

Ideally what I want to do is add the new functions I create in the child class and add them to the array.

Any thoughts?

John
Hi again

Re: "you can't do that with member function pointers beside you would cast them for storing them into the container and back again before calling"

actually, do you mean that you could cast a function from CMySecondView so that it could be stored in the array expecting a CMyView function? If that is possible, it may be exactly what I'm looking for.

Can you tell me how to do it if possible?

John
SOLUTION
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
Essentially, I have a number of functions (some which I would like to override in a child class) which I store in a CArray. The reason I want them stored in an array is that sometimes I want to call specific some functions and sometimes I don't. Storing the functions in an array means that I can easily access them using their index.

As I say, this works great when the functions are all in the base class. But I want to extend the number of functions by modifying the child class. The base class has loads of other elements and functions that are specific to the base class. All I want is an array of functions that can be added to by the base and derived class with the array storing these function pointers based in the base class.
SOLUTION
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
Think about it another way. I suppose I have 100 functions but I only want to call

function 1
function 27
function 33
function 25

(in that order!!! and the order and number of functions will change at runtime)

As far as I can see, and I guess I'm missing something if all the other experts think I'm going about it the wrong way, the easiest way to do it is to have the functions in an array. Otherwise, there is a massive number of combinations to list in a switch.

Any thoughts?

John
>>and the order and number of functions will change at runtime

Maybe you should tell us why and what it is for.
It is a simulation process. Highly mathematical; hence the need for many fuctions.

As I say, my original approach worked fine it is just this step of allowing functions to be defined in the child class that is causing the problem i.e. the function array is expectiung functions of type CMyView rather than the extended CMyNewView.

Is there no way for the function array to simply hold functions which don't relate to a class?

John
you could think to have a class which represent a function rather than multiple functions of a child class. then you could store instances of the function class as baseclass pointers and call

    functions[index]->exec(*this);

which then would be a virtual call.

slowly:

// baseclass
class Function
{
     // here you could define common members
public:
     virtual void exec(YourBaseClass & ybc) = 0;
};

class Function1 : public Function
{
public:
      void exec(YourBaseClass & ybc)
      {
            // do something or call back into your class 
      }
};

...

class YourBaseClass
{
     Array<Function * > functions;
public:
     YourBaseClass() { functions.resize(100, NULL); }
     void addFunction(int index, Function * pf)
     { functions[index] = pf; }
     void execFunction(int index)
     {
           if (functions[index] != NULL)
                functions[index]->exec(*this); 
       ..
};

Open in new window


Sara

SOLUTION
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
Thanks for all your help.

What I am going to do is define all the functions I need in the base class as virtual and then override them in the child class. It wasn't exactly what I required but it will suffice for now.

John
not exactly what was required but pointed me to a solution.