This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Why Gcc error(I only test it on 2.95 and later version)


//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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]