Using a pointer to call Constructor from inside a template

M

mike.arsenault

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
}
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
M

mike.arsenault

Alf said:
* (e-mail address removed):

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?

Hey Alf

This group has become a lot more snarky in the last 15 years or so,
when I was last on here :). I think I'm qualified to do this as I've
done it before - I've just forgotten the syntax. Perhaps someone else
can offer an answer that I can actually use!!

Mike
 
M

mike.arsenault

Hey Alf

This group has become a lot more snarky in the last 15 years or so,
when I was last on here :). I think I'm qualified to do this as I've
done it before - I've just forgotten the syntax. Perhaps someone else
can offer an answer that I can actually use!!

Mike

..... but thank you....

placement new, did the trick....
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top