Constructor problem

R

Robert

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.time = timed_float_array.time;

timed_float_key.tfkeys.nValues = timed_float_array
.tfkeys.nValues;

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

timed_float_key.tfkeys.fvalues = new float

[timed_float_key.tfkeys.nValues];

timed_float_key.tfkeys.fvalues[j] = (float)j;
//
timed_float_array.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
 
L

LR

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 = fkeys;
}

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.time = timed_float_array.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
 
B

Balog Pal

"Robert" <[email protected]>

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

replace to:

std::vector said:
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.
 
R

Robert

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
{

   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 = fkeys;

}

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.time = timed_float_array..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- Hide quoted text -

- Show quoted text -


Many thanks LR.

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

Robert

"Robert" <[email protected]>

class FloatKeys
{
public:> DWORD nValues;

 >float* fvalues;

replace to:



replace to:

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

This solves the few dozen problems your code currently have.

Thanks balog pal, look justwhat I need!
 

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,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top