首頁 > 軟體

分享C++三種型別new型別的運運算元使用詳情

2022-04-10 22:00:39

1.new操作符

new operator,平時用的最多的new操作符,其對應delete operator,不能被過載,其包含兩個操作(既申請空間,又呼叫建構函式)

  • (1)使用::operator new申請記憶體
  • (2)呼叫類別建構函式
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;
}

執行結果如下:

到此這篇關於C++三種型別new運運算元的使用詳情的文章就介紹到這了,更多相關C++ new運運算元內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com