Re: Enum plus std::vector questions
"Heinz Ozwirk" <SPAMhozwirk@arcor.de> ???g???l???s?D:4699feda$0$3842$9b4e6d93@newsspool4.arcor-online.net...
"Jack" <jl@knight.com> schrieb im Newsbeitrag
news:ORFnvusxHHA.2432@TK2MSFTNGP04.phx.gbl...
Question 1:
I use enum in my application
enum {
APPLE = 1,
ORANGE,
PEAR,
WATER_MELON
};
When I probe a variable of say APPLE, it just shows '1' rather than
'APPLE'... how do i fix that?
Write a function that accepts one of your enumeration values und displays
the appropriate text.
Question 2:
if I have a std::vector<FRUIT> fruitarr; ,
When I want to add a new item, do I just do a or b? Or both of them are
just fine
a) memcpy (fruitarr[count], fruit, sizeof(fruit));
b) or fruitarr[count] = fruit;
None of them adds a new item. Both (if written correctly) would assign a
new value to an existing item. Use std::vector<>::push_back (or other
members of vector) to add new items.
PFRUIT CFruitRec::addfruit(PFRUIT pfruit)
{
PFRUIT pf;
fruitarr.push_back(pfruit);
// cannot convert from PFRUIT to const const FRUIT&
// originally
// pf = memcpy (&fruitarr[numfruit], pfruit, sizeof(pfruit));
numfruit++;
return pf;
// just want to return the current item to the caller...
}
Thanks for any further hints...
Jack
Regarding memcpy -- avoid it whenever possible. If assignment works, as it
does above, use it. If it doesn't -- think why it doesn't, and think twice
before you try to use memcpy as a last resort. Remember, memcpy does not
know about user defined assignment operators, copy cobstructors, v-tables
or other C++ stuff.
Also, in your example, memcpy will not work, at least not as written in
line b. memcpy uses pointers, and at least fruitarr[count] is not a
pointer.
Heinz