copy derived structure, is it legal c++ code?

F

FFMG

Hi,

I am trying to copy some variables from a struct to a derived struct.
If I have something like,

....
struct STRUCTA
{
int numA;
long numB;
float numC;
}

struct STRUCTB public STRUCTA
{
int numD;
}
....

STRUCTA structA = {0,1,2};
STRUCTB structB;
memcpy( &structB, &structA, sizeof( STRUCTA) );

....
//
Is the above code legal to copy the structure from structA to structB

Thanks

F.
 
M

Marcel Müller

FFMG said:
I am trying to copy some variables from a struct to a derived struct.
If I have something like,
...
struct STRUCTA
{
int numA;
long numB;
float numC;
}

struct STRUCTB public STRUCTA
{
int numD;
}
[There are many syntactical errors up to now.]
...

STRUCTA structA = {0,1,2};
STRUCTB structB;
memcpy( &structB, &structA, sizeof( STRUCTA) );
...
//
Is the above code legal to copy the structure from structA to structB

It is not valid. It is undefined bahaviour.
STRUCTB is no longer a POD type because of the inheritance.

However, as long as neither STRUCTA or STRUCTB has virtual Methods or
things like that it usually works. But think what happens if STRUCTB is
defined as:

struct STRUCTB : public STRUCTC, public STRUCTA
{
int numD;
}

Your code will most likely fail badly.

And as far as I know there is no guarantee that the A part of B is in
the front of B. This is implementation defined.


But there is no need for the memcpy in your case at all since C++
supports slicing. B has also the properties of A, so you can assign the
A slice independantly. But this is no implicit conversion.

You must either explicitely address the A part by doing a cast

static_cast<STRUCTA&>structB = structA;

or you must call the assignment operator of STRUCTA explicitely

structB.STRUCTA::eek:perator=(structA);

I would prefer the first since I am unsure wether the second one is a
language extension of gcc.


Marcel
 
G

Gianni Mariani

FFMG said:
Hi,

I am trying to copy some variables from a struct to a derived struct.
If I have something like,

...
struct STRUCTA
{
int numA;
long numB;
float numC;
}

struct STRUCTB public STRUCTA
{
int numD;
}
...

STRUCTA structA = {0,1,2};
STRUCTB structB;
memcpy( &structB, &structA, sizeof( STRUCTA) );

...
//
Is the above code legal to copy the structure from structA to structB

I'm not sure, however, this is:

struct STRUCTA
{
int numA;
long numB;
float numC;
};

struct STRUCTB : STRUCTA
{
int numD;
};

int main()
{
STRUCTA structA = {0,1,2};
STRUCTB structB;
STRUCTA & rstructB = structB;
rstructB = structA;
}

You could probably make a template that did that for you. e.g.

template <typename T1, typename T2>
T2 & assign( T1 & v1, const T2 & v2 )
{
T2 & rv1 = v1;
return rv1 = v2;
}
 
J

James Kanze

FFMG wrote:
I'm not sure,

It's undefined behavior for two reasons. First, as Marcel
Müller points out, there is no guarantee that the address of
structA (converted to a void*) is the same as the address of the
STRUCTB subclass. Secondly, because there is no requirement
that the compiler allocate all of the memory of an object when
it is a base class. (In practice, I don't think any compilers
apply this optimization other than for an empty class,
allocating zero bytes, although sizeof( Class ) returns 1. But
the standard allows it any time a type is used as a base class.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top