c++ struct reference types
I have a class called IAckHandler
class IAckHandler {
public:
virtual ~IAckHandler();
virtual void handleAck(long messageType, bool ackrcvd, uint8_t ackByte) = 0;
private:
};
The intended use is for other classes to inherit, so this is basically a "interface" class.
I am using message queues to submit requests to a polling thread(uart comm) and want to pass in an IAckHandler into the message queue message. The struct is like so:
struct reqmsg
{
long int mtype;
void (*reqHandler)(MasterRadioComm*, uint8_t*);
开发者_开发知识库 IAckHandler* ackHandler;
unsigned char mtext[NUM_INDICES];
};
Here is the function to submit to message queue
void Uartcom::req_doSomething(IAckHandler& ackHandler)
{
struct reqmsg req;
if(ackHandler == NULL) req.ackHandler = this;
else req.ackHandler = ackHandler;
msgsnd(m_msgQueueKey, &req, sizeof(struct reqmsg) - sizeof(long), 0);
}
But when I pass in a reference to an IAckHandler, I get cannot convert ‘IAckHandler’ to ‘IAckHandler*’ in assignment.
Can I call req_doSomething(this) from another class that inherits IAckHandler?
You seem to be lacking some basic pointers and references knowledge. For the req_doSomething
given in the example, &ref
will give you a pointer to the referred object so it should be
else req.ackHandler = &ackHandler;
and to get a reference from a pointer, you derreference it with *ptr
so the call would be
req_doSomething(*this);
...or just drop the reference and work entirely with pointers.
Update: As others have pointed out, a reference cannot be NULL.
Change your function declaration to take the argument as a pointer rather than a reference:
void Uartcom::req_doSomething(IAckHandler * ackHandler)
^^^
You have a few issues with your req_doSomething function:
- ackHandler is a reference which cannot be NULL.
- You are trying to assign a reference to a pointer (
req.ackHandler = &ackHandler;
is what you want).
However, it's probably a better idea to do something like this:
void Uartcom::req_doSomething(IAckHandler * ackHandler)
{
struct reqmsg req;
if(ackHandler == NULL) req.ackHandler = this;
else req.ackHandler = ackHandler;
msgsnd(m_msgQueueKey, &req, sizeof(struct reqmsg) - sizeof(long), 0);
}
It describes exactly what you should do.
just past the object's address to the pointer:
req.ackHandler = &ackHandler;
Look carefully. The member in the struct is an "IAckHandler*" while the arg received in the setter function in a reference to IAckHandler. What you may want to do is:
req.ackHandler = &ackHandler;
regards,
Yati Sagade
It looks as if you must have declared ackHandler as an IAckHandler* in struct reqmsg. In C++, reference types (like IAckHandler&), although implemented using pointers, are treated syntactically as if they were embedded objects - basically you need to work with an IAckHandler& as if it were an IAckHandler, not an IAckHandler*.
Therefore, you should be able to fix your code like this:
void Uartcom::req_doSomething(IAckHandler& ackHandler)
{
struct reqmsg req;
if(ackHandler == NULL) req.ackHandler = this;
else req.ackHandler = &ackHandler;
msgsnd(m_msgQueueKey, &req, sizeof(struct reqmsg) - sizeof(long), 0);
}
精彩评论