일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- 선형모델
- 지도학습
- regression
- MapReduce
- 비선형모델
- inheritance
- virtual function
- 최소제곱법
- Overloading
- Nonlinear Model
- call by value
- c++
- call by reference
- kubernetes
- 회귀학습
- 회귀
- Effective C++
- 맵리듀스
- Class
- 딥러닝
- least square
- struct
- 머신러닝
- Machine learning
- member function
- Linear Regression
- Nonlinear Function
- local minimum
- overriding
- 비선형 함수
- Today
- Total
J's Study log
[C++] new & delete Operator Overloading 본문
new operator
There are three different of representative function of new operator.
- Memory Allocation
- Calling Constructor
- Type casting of returned address to appropriate type.
Overloading new operator actually means not the overloading every function of new operator, you just change how to allocate the memory.
In other words, the way of calling a constructor and type casting of returned address cannot be overloaded.
void* operator new(size_t size) {...}
This is the form of overloading 'new operator', return type must be 'void*' type and type of parameter must be 'size_t'.
Make sure that the return value of new overloading function is 'the address of allocated memory space'.
Next the compiler will call constructor and reset the allocated memory space.
And finally returns the constructed object's address after casting it to the class pointer.
The point is that the return value of new operator is not the return value of overloaded new function.
delete operator
Point* ptr = new Point(3, 4); //Point is a class
delete ptr;
When destructing an object the compiler first calls the destructor of the object which 'ptr' is pointing.
And it passes the address value of ptr to the function like this.
void operator delete(void* adr) {...} //The form of delete function
Overloaded delete function have responsible of destruction of the allocated memory space.
(Of course you can add more things too)
void operator delete (void* adr)
{
delete []adr;
}
If the compiler doesn't allow to operate delete for void* type, use type casting like this code.
void operator delete (void* adr)
{
delete []((char*)adr);
}
operator new[]
If you allocate array just put [ ] after overloaded function.
void* operator new[](size_t size) {...}
void* operator delete[](void* adr) {...}
delete []arr; //how to call overloaded delete[] function
When this code is compiled, the compiler calls destructor first.
And it passes the address value of 'arr' to function 'void operator delete[] (void* adr){...}' while calling it.
Example code
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0) : xpos(x), ypos(y) { }
friend ostream& operator<<(ostream& os, const Point& pos);
void * operator new (size_t size)
{
cout << "operator new : " << size << endl;
void * adr = new char[size];
return adr;
}
void * operator new[](size_t size)
{
cout << "operator new [] : " << size << endl;
void * adr = new char[size];
return adr;
}
void operator delete (void * adr)
{
cout << "operator delete ()" << endl;
delete[]adr;
}
void operator delete[](void * adr)
{
cout << "operator delete[] ()" << endl;
delete[]adr;
}
};
ostream& operator<<(ostream& os, const Point& pos)
{
os << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;
return os;
}
int main(void)
{
Point * ptr = new Point(3, 4);
Point * arr = new Point[3];
delete ptr;
delete[]arr;
return 0;
}
[참고자료]
- 윤성우의 열혈 C++ 프로그래밍
'Computer Language > C++' 카테고리의 다른 글
[C++] Class Template & Template Specialization (0) | 2020.07.06 |
---|---|
[C++] idea of Template & Function Template (2) | 2020.06.27 |
[C++] Operator Overloading - const/Index Operator of Array (2) | 2020.06.20 |
[C++] Shallow Copy & Deep Copy (0) | 2020.06.02 |
[C++] Operation Overloading - Assignment Operator (0) | 2020.05.30 |