467,879 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

Using a pointer to call Constructor from inside a template

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
}

Nov 20 '06 #1
  • viewed: 1728
Share:
3 Replies
* 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?
Nov 20 '06 #2

Alf P. Steinbach wrote:
* 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?
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

Nov 20 '06 #3

mi************@ivara.com wrote:
Alf P. Steinbach wrote:
* 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?

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....

Nov 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
40 posts views Thread by Elijah Bailey | last post: by
4 posts views Thread by Carsten SpieŖ | last post: by
1 post views Thread by Adam Dziendziel | last post: by
10 posts views Thread by Tony Johansson | last post: by
3 posts views Thread by Lawrence Spector | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.