Re: Getting struct fields info at runtime?
And also, even if I use inheritance, if in the new StructVersion2 instead
of
adding fields I have just to increment the length of a char[] field, if I
try to use an instruction such as this
StructVersion2(const StructVersion1& s)
{
*this = s;
}
what will be the behaviour of the assignment?
The position of the fields doesn't correspond, so will I get an error?
You mean you have something like this:
class CStructVersion1
{
char thestring[100];
int a;
}
class CStructVersion2 : public CStructVersion1
{
CStructVersion2(const CStructVersion1& s) { *this = s; }
char thestringlonger[110];
}
First you should have a operator= function in CStructVersion1 because
thestring is an array that must be strcpyd, so
const CStructVersion1 CStructVersion1::operator=(const CStructVersion1& s)
{
if (this!=&s) { strcpy(thestring,s.thestring); a = s.a; }; return *this;
}
In CStructVersion2 you achive with "*this=s" that the above operator=
function is called.
As I understand you, you want copy thestring of CStructVersion1 in
thestringlonger in CStructVersion2. Not to write everything new in the
constructor, you could perhaps only add strcpy(thestringlonger,
s.thestring).
CStructVersion2(const CStructVersion1& s) { *this = s;
strcpy(thestringlonger, s.thestring); }
HTH
Guido