error C2614: 'CYYYRegister' : illegal member initialization:
'CRequest' is not a base or member
Hello
I am trying to understand an issue which I know how to fix but don't
know why it works.
Getting this error:
error C2614: 'CYYYRegister' : illegal member initialization:
'CRequest' is not a base or member
Code is below. If I change class CXXXRequest slightly as below:
class CXXXRequest : public virtual CRequest
Then the error goes away.
Why? Why do I need the virtual keyword for compiler to realise that
CRequest is a base?
#include <string>
#include <iostream>
struct message_t
{
long id;
std::string strItem;
};
class TmMessage
{
public:
TmMessage(message_t *_msg) : msg(_msg) {}
message_t* GetMsg() const { return msg; }
protected:
message_t* msg;
};
class CRequest : public TmMessage
{
public:
CRequest::CRequest(int clientId, message_t *_msg)
: TmMessage(_msg)
{
std::cout << "CRequest::CRequest(int clientId, message_t *_msg)" <<
std::endl;
}
void CRequest::Options(unsigned idx, ...)
{
std::cout << "CRequest::Options" << std::endl;
}
};
class CXXXRequest : public /*virtual*/ CRequest
{
public:
CXXXRequest::CXXXRequest(int clientId, message_t *msg)
: CRequest(clientId, msg){}
int CXXXRequest::Invoke()
{
std::cout << "CXXXRequest::Invoke" << std::endl;
return 0;
}
};
class CYYYRegister : public CXXXRequest
{
public:
CYYYRegister(int clientId, message_t *msg) :
CRequest(clientId, msg),
CXXXRequest(clientId, msg)
{
std::cout << "CYYYRegister(int clientId, message_t *msg)" <<
std::endl;
}
int CYYYRegister::Invoke()
{
std::cout << "CYYYRegister::Invoke()" << std::endl;
}
};
int main()
{
int clientid = 1;
message_t mymsg;
mymsg.id = 7;
mymsg.strItem = "msg item";
CYYYRegister y(clientid, &mymsg);
CXXXRequest x(clientid, &mymsg);
message_t* pMsg = y.GetMsg();
int ret = x.Invoke();
return 0;
}