N
NewToCPP
Hi,
I see a problem with the virtual functions when I new an array of
elements of the derived class type and get them using the base class
type.
In the program given below
type = msg->getMsgFormatType();
will crash the second time it goes through the loop. First time it
works fine. Second time it has problem. when I debug the code I
observed the following
first time:
msg
__vfptr ===> DerivedMsg::'vftable'
second time:
msg
__vfptr ===> ERROR expression
Code:
=========
enum FormatType
{
common,
derived
};
class CommonMsg
{
public:
virtual FormatType getMsgFormatType();
};
FormatType CommonMsg::getMsgFormatType()
{
return common;
}
class DerivedMsg
ublic CommonMsg
{
public:
unsigned char msgBuffer[55];
FormatType getMsgFormatType();
};
FormatType DerivedMsg::getMsgFormatType()
{
return derived;
}
class CommonMsgQ
{
protected:
CommonMsg *msg;
public:
virtual CommonMsg* getElement(int);
};
CommonMsg* CommonMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&msg[index]);
else
return (CommonMsg*)0;
}
class DerivedMsgQ : public CommonMsgQ
{
public:
DerivedMsgQ (int);
CommonMsg* getElement(int);
};
DerivedMsgQ:
erivedMsgQ(int size)
{
msg = (CommonMsg *) new DerivedMsg[5];
}
CommonMsg* DerivedMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&msg[index]);
else
return (CommonMsg*)0;
}
int main()
{
DerivedMsgQ dMsg(3);
CommonMsg* msg = (CommonMsg*)0;
for (int i =0; i<3; ++i)
{
msg = (CommonMsg*) 0;
msg = dMsg.getElement(i);
if (msg != (CommonMsg*) 0)
type = msg->getMsgFormatType();
}
return 0;
}
I see a problem with the virtual functions when I new an array of
elements of the derived class type and get them using the base class
type.
In the program given below
type = msg->getMsgFormatType();
will crash the second time it goes through the loop. First time it
works fine. Second time it has problem. when I debug the code I
observed the following
first time:
msg
__vfptr ===> DerivedMsg::'vftable'
second time:
msg
__vfptr ===> ERROR expression
Code:
=========
enum FormatType
{
common,
derived
};
class CommonMsg
{
public:
virtual FormatType getMsgFormatType();
};
FormatType CommonMsg::getMsgFormatType()
{
return common;
}
class DerivedMsg
{
public:
unsigned char msgBuffer[55];
FormatType getMsgFormatType();
};
FormatType DerivedMsg::getMsgFormatType()
{
return derived;
}
class CommonMsgQ
{
protected:
CommonMsg *msg;
public:
virtual CommonMsg* getElement(int);
};
CommonMsg* CommonMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&msg[index]);
else
return (CommonMsg*)0;
}
class DerivedMsgQ : public CommonMsgQ
{
public:
DerivedMsgQ (int);
CommonMsg* getElement(int);
};
DerivedMsgQ:
{
msg = (CommonMsg *) new DerivedMsg[5];
}
CommonMsg* DerivedMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&msg[index]);
else
return (CommonMsg*)0;
}
int main()
{
DerivedMsgQ dMsg(3);
CommonMsg* msg = (CommonMsg*)0;
for (int i =0; i<3; ++i)
{
msg = (CommonMsg*) 0;
msg = dMsg.getElement(i);
if (msg != (CommonMsg*) 0)
type = msg->getMsgFormatType();
}
return 0;
}