Constructor problem

Discussion in 'C++' started by Robert, Dec 31, 2009.

  1. Robert

    Robert Guest

    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
    Robert, Dec 31, 2009
    #1
    1. Advertising

  2. Robert

    LR Guest

    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
    LR, Dec 31, 2009
    #2
    1. Advertising

  3. Robert

    Balog Pal Guest

    "Robert" <>

    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.
    Balog Pal, Dec 31, 2009
    #3
  4. Robert

    Robert Guest

    On Dec 31 2009, 4:50 am, LR <> wrote:
    > 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- Hide quoted text -
    >
    > - Show quoted text -


    Many thanks LR.

    Even with my current headache, I actually made it work!
    Robert, Jan 1, 2010
    #4
  5. Robert

    Robert Guest

    On Dec 31 2009, 11:33 am, "Balog Pal" <> wrote:
    > "Robert" <>
    >
    > 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.


    Thanks balog pal, look justwhat I need!
    Robert, Jan 1, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,025
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,318
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,797
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    453
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,190
Loading...

Share This Page