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
} 3 2010
* 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?
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 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.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel |
last post by:
Hello =)
I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -
self.setInterval('ObjectName.methodName()',...
|
by: Elijah Bailey |
last post by:
I want to sort a set of records using STL's sort() function,
but dont see an easy way to do it.
I have a
char *data;
which has size mn bytes where m is size of the record and
n is the...
|
by: jimjim |
last post by:
Hello,
My question concerns as to how a pointer is passed by reference as a
function argument. The following is from code taken from the MICO
implementation of the CORBA specification.
in...
|
by: Carsten Spieß |
last post by:
Hello all,
i have a problem with a template constructor
I reduced my code to the following (compiled with gcc 2.7.2) to show
my problem:
// a base class
class Base{};
// two derived...
|
by: Adam Dziendziel |
last post by:
Hi all!
I'm writing a luabind/boost::python-like binding utility for a Squirrel
language to generating wrapper-functions for C++ classes and have a
problem with passing the pointer-to-member...
|
by: Tony Johansson |
last post by:
Hello Experts!!
This class template and main works perfectly fine.
I have this class template called Handle that has a pointer declared as T*
body;
As you can see I have a reference counter in...
|
by: Raf256 |
last post by:
I have base template class A<B>, and son class B.
Inside A<B> constrcutor, can I access a pointer to B, from "this"?
like
A<B>::A() : pointerToB(static_cast<B*>this) { }
I will use the...
|
by: Bern McCarty |
last post by:
I am porting stuff from MEC++ syntax to the new C++/CLI syntax. Something
that we did in the old syntax that proved to be very valuable was to make
sure that the finalizer would purposefully...
|
by: Lawrence Spector |
last post by:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.
#include <iostream>
class TestClass
{
public:
template...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |