Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

manually calling a template argument constructor, How ???
Hi,

I am trying to manually call a constructor of a template argument, the
compiler returns “error C2039: ‘T’ : is not a member...”

How can I manually call the constructor of a template argument?

Please note that I cannot use the ‘new’ operator ( as long as it allocat
es
memory ), see the following code snnipet as an example of what I am trying t
o
do.


class HEADER {
LONG lStam;
HEADER() : lStam(0) {}
};

template< typename T >
class CMyTest
{
protected:
CMyTest();
~CMyTest();

typename T m_pObjects[0];
public:
static HRESULT CreateInstance(IN int iObjCount, OUT CMyTest** ppObj) {
*ppObj = (CMyTest*)malloc(sizeof(CMyTest) + sizeof(T) * iObjCount);
if(0 == *ppObj)
return E_OUTOFMEMORY;
(*ppObj)->CMyTest::CMyTest();// Works, calls the constructor
for(int i = 0; i < iObjCount; i++)
(*ppObj)->m_pObjects[0].T::T();// error C2039: 'T' : is not a member of
'HEADER'
}
};

int _tmain(int argc, _TCHAR* argv[])
{
CMyTest<HEADER>* pTest;
CMyTest<HEADER>::CreateInstance(10, &pTest);
}

The above code doesn’t have any practical meaning, it’s only goal is to
demonstrate the problem I am describing here

--
Nadav
http://www.sophin.com

Report this thread to moderator Post Follow-up to this message
Old Post
Nadav
04-12-08 12:25 AM


Re: manually calling a template argument constructor, How ???
Nadav wrote:
> Hi,
>
> I am trying to manually call a constructor of a template argument, the
> compiler returns "error C2039: 'T' : is not a member..."
>
> How can I manually call the constructor of a template argument?
>
> Please note that I cannot use the 'new' operator ( as long as it
> allocates memory ), see the following code snnipet as an example of
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Carl Daniel [VC++ MVP]
04-12-08 12:25 AM


Re: manually calling a template argument constructor, How ???
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:%23vp2Wo%23mIHA.3636@TK2MSFTNGP02.phx.gbl...
> Nadav wrote: 
>
> VC++ Extension, not portable C++ code.  There's no way to "call a
> constructor" in standard C++.
> 
>
> 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 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
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
Ben Voigt [C++ MVP]
04-12-08 12:25 AM


Re: manually calling a template argument constructor, How ???
Ben Voigt [C++ MVP] wrote:
> 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 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



Report this thread to moderator Post Follow-up to this message
Old Post
Carl Daniel [VC++ MVP]
04-12-08 09:50 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

VC++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:24 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.