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
IssacJonesAsked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
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.

virtual functions and member fucntion pointers are different concept. mfc uses both for example the OnInitDialog is virtually called message handler while functions like MyView::OnClickedBtn1 where called by (casted) function pointers. i would strongly recommend to use only the first method yourself.

Sara
0
 
sarabandeCommented:
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
0
 
IssacJonesAuthor Commented:
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
IssacJonesAuthor Commented:
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
0
 
AxterConnect With a Mentor Commented:
Even if you could achieve this, it would make it hard to debug the code.

I recommend using virtual functions to accomplish your goal.

It would help us to give you a better answer, if you could please give us details on what is your end goal.
I think there should be several solutions available which would not require function pointers.
0
 
IssacJonesAuthor Commented:
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.
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
virtual void PerformSomeAction(int iIndex)  //implemented in base and derived class
{
  switch(iIndex)
  {
     case 0:  Foo(); break;
     case 1:  Foo2(); Foo(); Break;
...
  }
};


ps.  I agree with the other experts - what you are doing will be difficult to debug and, I suspect, is a solution to a non existant problem.
0
 
IssacJonesAuthor Commented:
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
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>and the order and number of functions will change at runtime

Maybe you should tell us why and what it is for.
0
 
IssacJonesAuthor Commented:
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
0
 
sarabandeCommented:
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

0
 
sarabandeConnect With a Mentor Commented:
functions that don't relate to a class (instance) can be defined as static functions (what would make your function pointer definition much easier). but without class instances you couldn't make virtual calls beside you would pass the pointer of your class as argument to all the functions.

Sara
0
 
IssacJonesAuthor Commented:
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
0
 
IssacJonesAuthor Commented:
not exactly what was required but pointed me to a solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.