Re: Calling a constructor from template class
- From: "Heinz Ozwirk" <hozwirk@xxxxxxxx>
- Date: Sat, 2 Sep 2006 07:14:14 +0200
"FredsFriend" <FredsFriend@xxxxxxxxxxxxxxxxxxxxxxxxx> schrieb im Newsbeitrag news:C1AFDA92-C094-4B2F-AA6F-61BD67F5FCAE@xxxxxxxxxxxxxxxx
Hi,
I have a template class defined something like the following.
template <typename T>
class ArrayClass
I want this class to call the constructor on a peice of memory multiple
times (so as to reuse the memory for different objects).
The code to do this not using a template looks like this.
struct TempStruct
{
TempStruct() : i(0) { }
int i;
};
TempStruct t;
t.TempStruct::TempStruct();
Whatever this does, it is not valid C++ (not even if VC++ actually compiles it). The proper way to "call" a constructor is using placement new:
new(&t) TempStruct;
One would assume that the way to use this in a template would be
Perhaps you do, but not one.
template <typename T>
class ArrayClass
{
T t;
void RunConstructor()
{
// t.T::TempStruct(); // ignore this line for a bit // line one
t.T::T(); // line two
}
};
However this gives you a compiler error
That shold've happened for the non-template struct as well.
Heinz
.
- Follow-Ups:
- Re: Calling a constructor from template class
- From: FredsFriend
- Re: Calling a constructor from template class
- Prev by Date: Re: Calling a constructor from template class
- Next by Date: Re: Calling a constructor from template class
- Previous by thread: Re: Calling a constructor from template class
- Next by thread: Re: Calling a constructor from template class
- Index(es):