Trying to use function pointers in a class  
Author Message
KNachte





PostPosted: Visual C++ Language, Trying to use function pointers in a class Top

Hi all, i'm having a bit of a problem understanding Function pointers, but i think i might be able to put them to a good use here.

I have a class (CCanCCM3) that stores data internally. It gets that data from other classes (CCanRcvMessage) that get their unpackmessage called by an external manager, upon data ariving on the bus it is listening to. What happens now, is that for each message that can come in, they inherited it and only changed the message for which to listen on the bus, and the function that should get called when that message is received on the bus.

I would like to create a derived class that gets that message ID passed to it in the constructor, and also a function pointer to the function it needs to call.

Could anyone help me out on this

class CRcvMessageEx : public CCanRcvMessage
{

public:

CRcvMessageEx (CCanNodeExternal *ENode_p, uint32 pgn, Function pointer here) : CCanRcvMessage( pgn, ENode_p) { store function pointer here }

private:

virtual void UnpackMessage(char *data_p, uint32 data_size, uint8, bool, CAN_BUS_ID_t Bus_ID)
{
Call stored function pointer here
}

};

class CCanCCM3 : public CCanNodeExtKWPZED

{
public:
void UnpackStatus1 (char *data_p); // functions that need to be called, dependent on pgn number received
void UnpackStatus2 (char *data_p);
void UnpackStatus3 (char *data_p);
}

Thank you,

Kristof



Visual C++3  
 
 
Mike Danes





PostPosted: Visual C++ Language, Trying to use function pointers in a class Top

A pointer to function (in fact pointer to member in this case) it's not enough.You also need to pass a pointer to an instance of class CCanCCM3 to CRcvMessageEx constructor and you need to store that pointer too. That's because a pointer to member function is exactly that, a pointer to that function code. To call it you also need the instance for which you want to call it. Here's how you can do it:

class CCanCCM3 : public CCanNodeExtKWPZED

{
public:

void UnpackStatus1 (char *data_p); // functions that need to be called, dependent on pgn number received
void UnpackStatus2 (char *data_p);
void UnpackStatus3 (char *data_p);
}

class CRcvMessageEx : public CCanRcvMessage
{

CCanCCM3 *m_pCanCCM3;

void (CCanCCM3::*m_pStatusFunction)(char *); // pay attention to the declaration of a pointer to member, it may be a little confusing in the begining


public:

CRcvMessageEx (CCanNodeExternal *ENode_p, uint32 pgn, CCanCCM3 *pCanCCM3, void (CCanCCM3::*pStatusFunction)(char *))

: CCanRcvMessage( pgn, ENode_p)

{

m_pStatusFunction = pStatusFunction;

m_pCanCCM3 = pCanCCM3;

}

private:

virtual void UnpackMessage(char *data_p, uint32 data_size, uint8, bool, CAN_BUS_ID_t Bus_ID)
{

(m_pCanCCM3->*m_pStatusFunction)(data_p); // pay attention to the usage of a pointer to member, it may be a little confusing in the begining
}

};

Now this is perfectly valid code but I'm not sure that you really need pointer to members. I like them but many people tend to be confused by them so maybe using another idea would be better.

For example you can make 3 classes like CCanCCM3 each one with a virtual UnpackStatus function. All these classes inherit an abstract base class which declares UnpackStatus as abstract.

In this design all you need to do is to pass a pointer to an instance of one of these 3 classes and call UnpackStatus on it.


 
 
Simple Samples





PostPosted: Visual C++ Language, Trying to use function pointers in a class Top

Perhaps you are doing something similar to what I am doing. See Allocating a variable-length structure/class. Note that if that helps, then einaros deserves credit too, and be sure to look at his web site for more help. Most of the code I posted is mine but einaros provided the critical link; that the new operator has a "placement" option (that probably will not make sense to you until you read the thread).

If you need to store the data, then perhaps vector of vectors for rows and fields might help. Marius Bancila's sample is enough to get started with if you can use something like that.



 
 
KNachte





PostPosted: Visual C++ Language, Trying to use function pointers in a class Top

Thank you Mike,

I now understand how it works, i was missing the fact that i need to pass it the actual instance of the class on which to operate. I think i'll be rewriting quite some code before our next fieldtests :)