Malloc与Free不调用构造函数与析构函数

例子:

#include "stdafx.h"
#include <new>
#include <iostream>
using namespace std;

class Obj
{
public:
    Obj()
    {
        cout << "constructor" << endl;
    };
    ~Obj()
    {
        cout << "destructor" << endl;
    };
};

void use_malloc_and_free(void)
{
    Obj* a = (Obj*)malloc(sizeof(Obj));
    free(a);
};

void use_new_and_free(void)
{
    Obj* a = new Obj;
    delete a;
};

int _tmain(int argc, _TCHAR* argv[])
{
    //use_malloc_and_free();
    //use_new_and_free();
printf("\n"); return 0; }

 

posted @ 2014-02-20 11:03  XIAOSHUA  阅读(553)  评论(0编辑  收藏  举报