views:

120

answers:

2

If I have a class like this:

typedef union { __m128 quad; float numbers[4]; } Data

class foo
{
public:
    foo() : m_Data() {}

    Data m_Data;
};

and a class like this:

class bar
{
public:

   bar() : m_Data() {}

   foo m_Data;
}

is foo's constructor called when making an instance of bar?

Because when I try to use bar's m_Data's quad in bar it seems to be uninitialized, even though it has values in numbers[4]. :\

Specifically, this crashes:

m_Data.quad = _mm_mul_ps(m_Data.quad, a_Other.m_Data.quad)

Any help would be appreciated. :)

+2  A: 

You have to declare your constructor to be public, otherwise you are not allowing anyone to instantiate your class if you declare it as private member.

AraK
+1  A: 

Looks good for me. foo and bar are non-POD types because they have a constructor, so their members are guaranteed to be initialized after constructing.

Maybe the data is overwritten later through a memory leak?

How do you create the instance of bar?

codymanix