
Signature
Nadav
http://www.sophin.com
> Hi,
>
[quoted text clipped - 7 lines]
> what I am trying to do.
> (*ppObj)->CMyTest::CMyTest();// Works, calls the constructor
VC++ Extension, not portable C++ code. There's no way to "call a
constructor" in standard C++.
> for(int i = 0; i < iObjCount; i++)
> (*ppObj)->m_pObjects[0].T::T();// error C2039: 'T' : is not a
> member of 'HEADER'
Here the extension fails.
> The above code doesn't have any practical meaning, it's only goal is
> to demonstrate the problem I am describing here
The correct way to do this is to use "placement new":
#include <new>
// (*ppObj)->CMyTest::CMyTest(); // Non-standard VC++ extension
new (*ppObj) CMyTest(); // standard, placement-new solution
// (*ppObj)->m_pObjects[0].T::T(); // extension breaks down
new (*ppObj) T(); // standard, placement-new solution
-cd
Ben Voigt [C++ MVP] - 12 Apr 2008 00:16 GMT
>> Hi,
>>
[quoted text clipped - 16 lines]
>
> Here the extension fails.
I think I once found that using a typedef solves the problem as well (and
solves the problem of how to invoke the destructor as well, so even if you
construct with placement new you may still need the trick.
typedef typename T TValue;
TValue* pT = (TValue*)malloc(sizeof(TValue));
pT->TValue(); or pT->TValue::TValue();
...
pT->~TValue(); or pT->TValue::~TValue();
>> The above code doesn't have any practical meaning, it's only goal is
>> to demonstrate the problem I am describing here
[quoted text clipped - 12 lines]
>
> -cd
Carl Daniel [VC++ MVP] - 12 Apr 2008 06:10 GMT
> I think I once found that using a typedef solves the problem as well
> (and solves the problem of how to invoke the destructor as well, so
[quoted text clipped - 5 lines]
> ...
> pT->~TValue(); or pT->TValue::~TValue();
The difference is, for the destructor
template<class T> ...
T* pt = ...
pt->~T();
is required to work by the C++ standard (at least, I'm pretty sure it's
required to work!), and does work under VC++ 2005 (I didn't try anything
older). The typedef trick may be required for some older versions - I don't
have anything older installed to try it out.
-cd