*
mi************@ivara.com:
Hello
I need some help from anyone that can provide it. Below is a function
inside a template collection class that I'm writing. I have a TYPE *
that points to an allocated memory location, and I'm explicitly trying
to call the TYPE::TYPE() constructor on it by using the pointer. That
doesn't compile, yet if I forget the usage of TYPE and write the line
to use the actual constructor, then it's fine.
See the code below for an example.
BTW, I have code in another function that uses the same mechanism to
call the destructor and that works with no problem.
pObjToDelete->~TYPE(); // call the destructor on the object to be
erased
Am I doing something wrong?? Any clues would be appretiated.
Thanks
Mike
template <class TYPE>
Coll<TYPE& Coll<TYPE>::addLast(const TYPE & anObject)
{
// I'm casting this pointer to the same type as the template
// parameter, but I know that points to a block of memory of
// proper size for TYPE
TYPE * pNewElement = getFreeElementFromBlocks();
// I get a compile error on this line. If the type that I pass in is
'String', then
// the error message is: 'TYPE' : is not a member of 'String'
pNewElement->TYPE::TYPE( anObject ); // call the copy constructor
// this doesn't work either
pNewElement->TYPE( anObject ); // call the copy constructor
// If I replace it with this EXPLICIT call, then everything is fine,
except
// when I try to use this template with another type
pNewElement->String::String( anObject ); // call the copy
constructor
}
The short answer is that since you don't know how to this, you're doing
something you're not qualified to do (sort of like an airplane pilot
asking how to start the plane) -- unless it's a learning exercise.
Assuming it's a learning exercise, use placement new.
Otherwise, don't.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?