Re: Calling a constructor from template class

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"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

.