> Waylander,
>
> Your class "TBStructure" contains no member variables. Your
> constructor is initializing a bunch of global variables defined in
> TBStructure.cpp, which is why all your objects appear to be the same.
>
Good catch! I hadn't noticed that detail.
> Each variable that you want to associate with an instance of a class
> must be declared in the class definition, ie.:
>
> class TBStructure
> {
> public:
> TBStructure();
> ~TBStructure();
>
> private:
> int x, y, width, height, id;
> };
>
> Then, you must initialize this data in the constructor
>
> TBStructure::TBStructure( in xI, int yI, int widthI, int heightI, int
> idI)
> : x( xI ), y( yI ), width( widthI ), height( heightI ), id( idI )
> {
> }
>
> all that stuff after the colon is called the "member initialization
> list". Look it up in your favorite C++ book, or google it.
>
The initialization of the member variables can be done using
assignment statements as well, though. Which one is better really is a
discussion for a C++ forum, as it's not really palm-specific.
> The change from this:
>
> (*(Fields+numFields)) = TBStructure(string2int
> (tbP->X),string2int(tbP->Y),
> string2int(tbP->Width),string2int(tbP-
> >Height), lastID++);
>
> to using 'new' as Dean suggested was correct. The line above will
> call the "copy constructor" for the uninitialized object on the left
> hand side of the equal sign. This is bad. Look up or google "copy
> constructor". You haven't defined one in your class, so the compiler
> happily creates one for you, which probably won't do what you want
> when there are member pointer member variables involved.
>
> All of these things are really out of scope for a Palm developer
> forum. This stuff really belongs to a C++ forum. But I'd be happy
> to answer any C++ questions off of the list.
Thanks for catching that the original statement was calling a copy
constructor; I guess my C++ is a trifle rustier than I'd thought.
Dean
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/