我知道应该有一个删除运算符,我不会在某个地方关注它.我只是想知道,哇,它有效. “大小”的论点来自何处?
#include<iostream> #include<string> class Base { public: Base() { } void *operator new( unsigned int size,std::string str ) { std::cout << "Logging an allocation of "; std::cout << size; std::cout << " bytes for new object '"; std::cout << str; std::cout << "'"; std::cout << std::endl; return malloc( size ); } private: int var1; double var2; }; int main(int argc,char** argv){ Base* b = new ("Base instance 1") Base; }
结果如下:
为新对象’Base instance 1’记录16字节的分配