Why Gcc error(I only test it on 2.95 and later version)
Dave Raggett
dave_raggett@hotmail.com
Wed Aug 8 22:59:00 GMT 2001
//my question is How to invoke a class's constructor outside the class
scope?
//Must I use placement new?Is there any other method?
#include <iostream.h>
#include <malloc.h>
class CTest
{
public:
CTest(int initValue=0):m_nData(initValue)
{
cout<<"CTest Construct"<<endl;
}
~CTest(){cout<<"CTest Destruct"<<endl;}
private:
int m_nData;
};
CTest* Make_CTest_ByMalloc(int initValue)
{
CTest* pTest=(CTest*)malloc(sizeof(CTest));
if(pTest)
pTest->CTest::CTest(initValue);//this will cause error
//and of course I can use placement new
//,but I want to know other method except
//placement new.
}
int main()
{
CTest* pTest=Make_CTest_ByMalloc(4);
if(pTest)free(pTest);
return 1;
}
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
More information about the Gcc
mailing list