Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Constructor problem

0 views
Skip to first unread message

Robert

unread,
Dec 30, 2009, 10:39:40 PM12/30/09
to

I am trying to create dynamic constructors but have encountered a
problem.

I am trying to populate a floating point array as part of the class
structure.

But seemingly what ever I try returns the same result.

Which seems to be all data that is not an array, gets passed through
ok.

But if the variable is an array, in this case an array of floats, then
the values are not created.

I figure that, you may be able to help . . .

So, firstly, I have these classes, followed by the code and then the
debug output.

Any help will be very much appreciated.

Rob.
/////////////////////////
class FloatKeys
{

public:

DWORD nValues;
float* fvalues;


FloatKeys( DWORD n_type, float *f );


FloatKeys();
~FloatKeys();


};

FloatKeys::FloatKeys( DWORD n_type, float* fkeys )
{
nValues = n_type;

fvalues = new float [n_type];


fvalues[0] = fkeys[0] ;

fvalues[1] = fkeys[1] ;

fvalues[2] = fkeys[2] ;


}

FloatKeys::FloatKeys()
{

}


FloatKeys::~FloatKeys()
{

}


/////////////////////////////////
class TimedFloatKeys
{

public:

DWORD time;
FloatKeys tfkeys;

TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey );

TimedFloatKeys();
~TimedFloatKeys();

};


TimedFloatKeys::TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey )
{

time = dw_time;

tfkeys.fvalues = new float [3];

tfkeys = fltkey;


}

TimedFloatKeys::TimedFloatKeys()
{

}

TimedFloatKeys::~TimedFloatKeys()
{

}

////////////////////////////////
class Animation_key {

public:

DWORD keyType;

DWORD nKeys;

TimedFloatKeys* timed_float_key;

Animation_key( DWORD dw_type, DWORD n_Key, TimedFloatKeys*

timed_float_array );

Animation_key();
~Animation_key();


};

Animation_key::Animation_key( DWORD dw_type, DWORD n_Key,
TimedFloatKeys*

timed_float_array )
{

DWORD j = 0;

DWORD i = 0;

keyType = dw_type;

nKeys = n_Key;


timed_float_key = new TimedFloatKeys [n_Key];

for( i = 0; i < n_Key; i++ )
{

timed_float_key[i].time = timed_float_array[i].time;

timed_float_key[i].tfkeys.nValues = timed_float_array
[i].tfkeys.nValues;

for( j = 0; j < timed_float_key[j].tfkeys.nValues; j++ )
{

timed_float_key[i].tfkeys.fvalues = new float

[timed_float_key[i].tfkeys.nValues];

timed_float_key[i].tfkeys.fvalues[j] = (float)j;
//
timed_float_array[i].tfkeys.fvalues[j];

}

}

}

Animation_key::Animation_key()
{

}

Animation_key::~Animation_key()
{

}

// I try to invoke the classes with this code.

HRESULT ffile_handling::Save_Animation_Keys( )
{


HRESULT hr;

DWORD cbsize = 64;

char output[255];

float* f_key = new float [3];

f_key[0] = 1.0f;

f_key[1] = 5.0f;

f_key[2] = 9.0f;

DWORD t = 24;

// FLOAT KEY

FloatKeys my_flt( 3, f_key );


// TIMED FLOAT KEYS

TimedFloatKeys* tfk_ptr;

TimedFloatKeys my_timed_flts( t, my_flt );

tfk_ptr = &my_timed_flts;
;


// ANIMATION KEY

Animation_key a_key(2, 1, tfk_ptr );

// BREAK POINT –

the output as seen in the debugger is this:

[] a_key {…}
|
|---key_Type 2
|---nKeys 1
|---[] timed_float_key 0x00cb2204
|-------time 24
|---[] tfkeys { … }
|---nValues 3
|---[] fvalues 0x00cb2140
|-- -4.31602e+008

LR

unread,
Dec 30, 2009, 11:50:05 PM12/30/09
to
Robert wrote:
[snippage]

>
> I am trying to create dynamic constructors but have encountered a
> problem.

I'm not sure what you mean by dynamic here.


>
> I am trying to populate a floating point array as part of the class
> structure.

> Which seems to be all data that is not an array, gets passed through
> ok.

Pointers too?

>
> But if the variable is an array, in this case an array of floats, then
> the values are not created.

Created or copied?


>
> I figure that, you may be able to help . . .

Have you thought about using std::vector?

> /////////////////////////
> class FloatKeys
> {
>
> public:
>
> DWORD nValues;
> float* fvalues;
>
>
> FloatKeys( DWORD n_type, float *f );

No copy ctor?
FloatKeys(const FloatKeys &);
>
>
> FloatKeys();
> ~FloatKeys();
>
>
> };
>
> FloatKeys::FloatKeys(DWORD n_type, float* fkeys )


> {
> nValues = n_type;
> fvalues = new float [n_type];
> fvalues[0] = fkeys[0] ;
> fvalues[1] = fkeys[1] ;
> fvalues[2] = fkeys[2] ;

Your code implies that n_type will always be at least 3. Is this true?
If it's possible for it to be some other value, then you might be better
off writing you code like:

FloatKeys::FloatKeys(DWord n_type, float *fkeys)
:
nValues(n_type),
fvalues(new float[n_type])
{
for(int i=0; i<n_type; i++)
fvalues[i] = fkeys[i];
}

> }

OTOH, if you know that n_type is going to be three each and every time,
why bother with the pointer in FloatKeys? Why not just make an array?
class FloatKeys {
float fvalues[3];
public:
FloatKeys() {
fvalues[0] = 0.;
fvalues[1] = 0.;
fvalues[2] = 0.;
}
....
};

> /////////////////////////////////
> class TimedFloatKeys
> {
> public:
> DWORD time;
> FloatKeys tfkeys;
>
> TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey );

No copy ctor here either?


>
> TimedFloatKeys();
> ~TimedFloatKeys();
> };
>
>
> TimedFloatKeys::TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey )
> {
> time = dw_time;
> tfkeys.fvalues = new float [3];

This seems like a very awkward place to be doing this. I think you'd be
better off with a copy ctor for FloatKeys, and writing your ctor like this:

TimedFloatKeys::TimedFloatKeys(DWORD dw_time, const FloatKeys &fltkey)
:
time(dw_time),
tfkeys(fltkey)
{}


> tfkeys = fltkey;

You just clobbered the value that you just put in tfkeys.fvalues. Now
you'll have a memory leak too.

It's not clear to me if you want to do a deep or shallow copy in
TimedFloatKeys.
http://www.lmgtfy.com/?q=deep+and+shallow+copy


> ////////////////////////////////
> class Animation_key {
> public:
>
> DWORD keyType;
> DWORD nKeys;
> TimedFloatKeys* timed_float_key;

Why is that a pointer to a timed_float_key, instead of an instance? Do
you want this to point to an array of TimedFloatKeys?


>
> Animation_key( DWORD dw_type, DWORD n_Key, TimedFloatKeys*
>
> timed_float_array );

If you have copy ctors for the other classes, easier to write
Animation_key(DWORD dw_type, DWORD n_key, const TimedFloatKeys &tfa)
:
keyType(dwType),
nKeys(n_key),
timed_float_key(&tfa)
{}

>
> Animation_key();
> ~Animation_key();
>
>
> };
>
> Animation_key::Animation_key( DWORD dw_type, DWORD n_Key,
> TimedFloatKeys*
>
> timed_float_array )
> {
>
> DWORD j = 0;
>
> DWORD i = 0;
>
> keyType = dw_type;
>
> nKeys = n_Key;
>
>
> timed_float_key = new TimedFloatKeys [n_Key];
>
> for( i = 0; i < n_Key; i++ )
> {
>
> timed_float_key[i].time = timed_float_array[i].time;

[rest snipped]

I suggest that 1) you look up deep and shallow copy, 2) get FloatKeys to
work with both an assignment operator and a copy ctor even if you don't
need them, 3) look at std::vector which may be a help to you.

LR

Balog Pal

unread,
Dec 31, 2009, 6:33:54 AM12/31/09
to
"Robert" <watki...@hotmail.com>

class FloatKeys
{
public:
> DWORD nValues;
>float* fvalues;

replace to:

std::vector<float> fvalues;

>FloatKeys::FloatKeys( DWORD n_type, float* fkeys )
>{
>nValues = n_type;

>...

replace to:

FloatKeys::FloatKeys( size_t size, const float* fkeys )
: fvalues(fkeys, fkeys+size)
{}

This solves the few dozen problems your code currently have.


io_x

unread,
Jan 1, 2010, 3:23:45 AM1/1/10
to
"Robert" <watki...@hotmail.com> ha scritto nel messaggio
news:5dcb4b84-5d57-4f31...@k19g2000yqc.googlegroups.com...

i speak for me:

your exmple not show any indentation, the code not compile too.
so if you have some problem with one part of code it is better
you cut the most little peace of it that compile, run,
and self-show the problem.

when you tell something in a programming group,
do it preferibily in few words (it has not matter if they are
not spell perfect (at last for me)).

Robert

unread,
Jan 1, 2010, 7:59:20 AM1/1/10
to
> TimedFloatKeys.http://www.lmgtfy.com/?q=deep+and+shallow+copy
> LR- Hide quoted text -
>
> - Show quoted text -

Many thanks LR.

Even with my current headache, I actually made it work!

Robert

unread,
Jan 1, 2010, 8:00:20 AM1/1/10
to
On Dec 31 2009, 11:33 am, "Balog Pal" <p...@lib.hu> wrote:
> "Robert" <watkins...@hotmail.com>

Thanks balog pal, look justwhat I need!

0 new messages