Re: Explicitly calling the constructor



Already solved.

The necessity was to place the scope operator in front of the constructor
call, as in the following example:

pData->CTestClass::CTestClass();

-------

"Antti Keskinen" <antti.keskinen@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:ebU%23fgHfFHA.3692@xxxxxxxxxxxxxxxxxxxxxxx
> 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;
> }
>


.



Relevant Pages

  • Re: a method to make js have the ability to inherit
    ... but without the implied type-conversion of the string ... that uses the name of a specific constructor. ... programmer has no idea at all what types of object they are ... no reason for ever doing so. ...
    (comp.lang.javascript)
  • Re: End-of-the-week fun
    ... Constructor and destructor names may be surrounded by parenthesis. ... > real compelling reason, ... > using namespace std; ... > int main ...
    (comp.lang.cpp)
  • Re: why cant this be a feature in C++?
    ... Paul wrote: ... > int main ... > Is there any reason for not having some thing like this, ... Array with elements initialised with a non-default constructor was ...
    (comp.lang.cpp)
  • Re: Structs cannot contain explicit parameterless constructors
    ... I don't know what the real reason is but it may have something ... constructor it would not be called unless you recompiled. ... much influence on the language design. ... IdentityStamp id = obj.Id; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Copy constructor needed I think, maybe some op= as well?
    ... > In your case that means: Why on earth do you allocate the Viewport object ... > in RadioButton dynamically? ... As far as I can see there is no good reason ... > happens to the pointer inner_button, if the default constructor is used?) ...
    (comp.lang.cpp)