Ví dụ và lý thuyết trong sách nào về C++ cũng có, đọc sách bạn sẽ thấy dễ hiểu hơn đó.
Em đang tìm hiểu về nạp chồng cho toán tử new và delete cho 1 lớp cụ thể. Ai biết có thể chỉ cho em cú pháp + ví dụ đơn giản (vì em mới học). Thanks
Ví dụ và lý thuyết trong sách nào về C++ cũng có, đọc sách bạn sẽ thấy dễ hiểu hơn đó.
Example
C++ Code:
#include <cstdlib> //declarations of malloc and free #include <new> #include <iostream> using namespace std; class C { public: C(); void* operator new (size_t size); //implicitly declared as a static member function void operator delete (void *p); //implicitly declared as a static member function }; void* C::operator new (size_t size) throw (const char *){ void * p = malloc(size); if (p == 0) throw "allocation failure"; //instead of std::bad_alloc return p; } void C::operator delete (void *p){ C* pc = static_cast<C*>(p); free(p); } int main() { C *p = new C; // calls C::new delete p; // calls C::delete }