亲宝软件园·资讯

展开

C++ new运算符

hsy12342611 人气:0

1.new操作符

new operator,平时用的最多的new操作符,其对应delete operator,不能被重载,其包含两个操作(既申请空间,又调用构造函数)

class CTestUse
{
public:
    CTestUse() { cout << __FUNCTION__ << endl; }
    virtual ~CTestUse() { cout << __FUNCTION__ << endl; }
 
private:
};
 
void test()
{
    string *pStr = new string("new operator");
    cout << *pStr << endl;
    delete pStr;
 
    CTestUse *pCase = new CTestUse;
    delete pCase;
}
 
 
int main(int argc, char *argv[])
{
    test();
 
    return 0;
}

运行结果如下:

2.::operator new

::operator new,只会申请空间,不会调用构造函数,可以被重载,其对应 ::operator delete (只申请空间)

2.1原始调用

class CTestUse1
{
public:
    CTestUse1() { cout << __FUNCTION__ << endl; }
    virtual ~CTestUse1() { cout << __FUNCTION__ << endl; }
    void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
 
private:
};
 
 
void test1()
{
    CTestUse1 *pCase = (CTestUse1 *)::operator new (sizeof(CTestUse1));
    pCase->show();
    ::operator delete(pCase);
 
}
 
int main(int argc, char *argv[])
{
    test1();
 
    return 0;
}

运行结果如下:

2.2重载1

class CTestUse2
{
public:
    CTestUse2() { cout << __FUNCTION__ << endl; }
    virtual ~CTestUse2() { cout << __FUNCTION__ << endl; }
    void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
 
    void* operator new(size_t size) {
        cout << __FUNCTION__ << endl;
        return ::operator new(size);
    }
 
    void operator delete(void* ptr) {
        cout << __FUNCTION__ << endl;
        ::operator delete(ptr);
    }
 
private:
};
 
void test2()
{
    CTestUse2 *pCase = new CTestUse2;
    pCase->show();
    delete pCase;
 
}
 
int main(int argc, char *argv[])
{
    test2();
 
    return 0;
}

运行结果如下:

2.3重载2

class CTestUse3
{
public:
    CTestUse3() { cout << __FUNCTION__ << endl; }
    virtual ~CTestUse3() { cout << __FUNCTION__ << endl; }
    void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
 
    void* operator new(size_t size, string str) {
        cout << __FUNCTION__ << str << endl;
        return ::operator new(size);
    }
 
    void operator delete(void* ptr) {
        cout << __FUNCTION__ << endl;
        ::operator delete(ptr);
    }
 
private:
};
 
void test3()
{
    CTestUse3 *pCase = new ("heshiyang") CTestUse3;
    pCase->show();
    delete pCase;
 
}
 
int main(int argc, char *argv[])
{
    test3();
 
    return 0;
}

运行结果如下:

3.place new

place new在已经构建好的内存中创建对象,其只会返回已经申请好的内存指针,多用在高性能场景下,提前申请好内存,可以节省申请内存开消 (只调用构造函数)

class CTestUse4
{
public:
    CTestUse4() { cout << __FUNCTION__ << endl; }
    virtual ~CTestUse4() { cout << __FUNCTION__ << endl; }
    void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
 
    void* operator new(size_t size, void* p) {
        cout << __FUNCTION__ << endl;
        return p;
    }
 
    void operator delete(void* ptr) {
        cout << __FUNCTION__ << endl;
        ::operator delete(ptr);
    }
 
private:
};
 
void test4()
{
    char* pChar = new char[sizeof(CTestUse4)];
    CTestUse4* pCase = new (pChar) CTestUse4; //调用全局::new (pChar) CTestUse4,new (pChar) CTestUse4调用重载new
    //delete pCase;
    pCase->~CTestUse4();
}
 
int main(int argc, char *argv[])
{
    test4();
 
    return 0;
}

运行结果如下:

加载全部内容

相关教程
猜你喜欢
用户评论