This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Why Gcc error(I only test it on 2.95 and later version)
- To: gcc at gcc dot gnu dot org
- Subject: Why Gcc error(I only test it on 2.95 and later version)
- From: "Dave Raggett" <dave_raggett at hotmail dot com>
- Date: Thu, 09 Aug 2001 05:59:20 +0000
//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