📜  C++ |运算符重载|问题10

📅  最后修改于: 2021-05-25 23:27:51             🧑  作者: Mango

以下哪个运算符不能是全局的,即必须是成员函数。
(一)
(二)删除
(C)对话运算符
(D)以上全部答案: (C)
说明: new和delete可以是全局的,请参见以下示例。

#include
#include
#include

using namespace std;

class Myclass {
    int x;
public:
    friend void* operator new(size_t size);
    friend void operator delete(void*);
    Myclass(int i) {
        x = i;
        cout << "Constructor called \n";
    }
    ~Myclass() { cout << "Destructor called \n"; }
};


void* operator new(size_t size)
{
    void *storage = malloc(size);
    cout << "new called \n";
    return storage;
}

void operator delete(void *p )
{
    cout<<"delete called \n";
    free(p);
}

int main()
{
    Myclass *m = new Myclass(5);
    delete m;
    return 0;
}

这个问题的测验

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。