Explicitly calling the constructor
- From: "Antti Keskinen" <antti.keskinen@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 29 Jun 2005 09:59:26 +0300
Hello !
I have a simple but interesting problem. I am using the 'malloc' routine to
reserve memory for a class object, and then trying to call the object's
constructor via the pointer returned. For some reason, this does not work at
all, and I have no idea why.
The reason why I need to do this is that I'm developing a COM component, and
in there it's suggested to use CoTaskMemAlloc to reserve memory instead of
the 'new keyword'. The object I'm trying to initialize is a complex class,
thus I need to call it's constructor.
Here is a code sample, to which Visual Studio .NET 2003 answers with error
C2273:
#include <stdlib.h>
#include <malloc.h>
#include <queue>
class CTestClass
{
public:
CTestClass()
{
m_nData = 0;
}
private:
int m_nData;
};
int main(void)
{
CTestClass* pData = (CTestClass*) malloc( sizeof(CTestClass) );
if ( pData )
{
pData->CTestClass();
free( pData );
}
return 0;
}
.
- Follow-Ups:
- Re: Explicitly calling the constructor
- From: Alexander Grigoriev
- Re: Explicitly calling the constructor
- From: Roland Frank
- Re: Explicitly calling the constructor
- From: adebaene
- Re: Explicitly calling the constructor
- From: Antti Keskinen
- Re: Explicitly calling the constructor
- Prev by Date: Re: Printing More than One Page (with graphics)
- Next by Date: Re: Explicitly calling the constructor
- Previous by thread: Printing More than One Page (with graphics)
- Next by thread: Re: Explicitly calling the constructor
- Index(es):
Relevant Pages
|