initilization of composite types

M

ma740988

Given a composite type such that:

struct tp2
{
unsigned int val1;
tp2 ()
: val1(0)
{}
};

And yet again another composite type that's is comprised of the the one
above.

struct tp1 {
unsigned int val2;
tp2 tp;
tp1()
: val2(0)
{}
};

Besides the fictious names, thats the general constructs for messaging
between my different systems.

What's interesting to me though is the fact that if I transfer tp1
between a source and destination, the struct tp2 in tp is
'unitialized'. In other words, val1 in tp2 is 'non zero' ( some
FFFFFF nonesense). This leads me to believe the initilzation of val1
in tp2 is not getting honored which is puzzling.

So I take it a step futher. So now:

struct tp1 {
unsigned int val2;
tp2 tp;
tp1()
: val2(0)
, tp() //<-- lets initialize tp here.
{}
};

This works out better. i.e val1 is now zero. This approach though
suggest that I'm initializing tp twice. Yes/No?

I'm using gcc 2.96 ( which I'm forced to live with for now ) but I'm
just trying to get a feel for why the first option didn't work out.

I suspect in this day and age, what I need to do is something akin to:

struct tp2
{
unsigned int val1;
tp2 ( int v )
: val1(v)
{}
};

struct tp1
{
unsigned int val2;
tp2 tt;
tp1 ( const tp2& t, int v )
: tt(t)
, val2 ( v )
{}
};
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top