티스토리 뷰
Reading Books/Effective Series
EC++ Item 21: Don't try to return a reference when you must return an object
DWGoon 2009. 12. 5. 22:07
// Coded with Dev-C++ IDE
// When you use a static variable in the case...
#include <iostream>
using namespace std;
class Glass {
public:
Glass() { cout<< "ctor.." << endl; }
Glass(int x, int y)
{
cout<< "ctor.." << endl;
this->x = x;
this->y = y;
}
friend Glass& operator+(const Glass& lhs, const Glass& rhs);
friend bool operator==(const Glass& lhs, const Glass& rhs);
int x;
int y;
};
Glass& operator+(const Glass& lhs, const Glass& rhs)
{
static Glass g;
g.x = lhs.x+rhs.x;
g.y = lhs.y+rhs.y;
cout << "g.x : " << g.x << ", g.y: " << g.y << endl;
return g;
}
bool operator==(const Glass& lhs, const Glass& rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
int main()
{
Glass g1(1, 3);
Glass g2(1, 4);
Glass g3(2, 2);
Glass g4(2, 2);
if ((g1+g2) == (g3+g4)) {
cout << "equal" << endl;
} else
cout << "not equal" << endl;
system("pause");
return 0;
}
// When you use a static variable in the case...
#include <iostream>
using namespace std;
class Glass {
public:
Glass() { cout<< "ctor.." << endl; }
Glass(int x, int y)
{
cout<< "ctor.." << endl;
this->x = x;
this->y = y;
}
friend Glass& operator+(const Glass& lhs, const Glass& rhs);
friend bool operator==(const Glass& lhs, const Glass& rhs);
int x;
int y;
};
Glass& operator+(const Glass& lhs, const Glass& rhs)
{
static Glass g;
g.x = lhs.x+rhs.x;
g.y = lhs.y+rhs.y;
cout << "g.x : " << g.x << ", g.y: " << g.y << endl;
return g;
}
bool operator==(const Glass& lhs, const Glass& rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
int main()
{
Glass g1(1, 3);
Glass g2(1, 4);
Glass g3(2, 2);
Glass g4(2, 2);
if ((g1+g2) == (g3+g4)) {
cout << "equal" << endl;
} else
cout << "not equal" << endl;
system("pause");
return 0;
}
ctor..
ctor..
ctor..
ctor..
ctor..
g.x : 4, g.y: 4
g.x : 2, g.y: 7
equal
ctor..
ctor..
ctor..
ctor..
g.x : 4, g.y: 4
g.x : 2, g.y: 7
equal
'Reading Books > Effective Series' 카테고리의 다른 글
EC++ Item 25: Consider support for a non-throwing swap (0) | 2009.12.05 |
---|---|
EC++ Item 9: Never call virtual functions during construction or destruction (0) | 2009.11.30 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- QPrinter.Letter
- pandas
- 이상한 문자
- GSX 1000 pro
- destructor
- CanDrA
- dll
- matrix multiplication
- QPrinter.A4
- TensorBoard
- how to solve it
- 볼륨 낮춤
- Accelerated C++
- Item 9
- GSX 1200 pro
- Python
- QT
- 설치
- structure
- TCGA
- PyQt
- volume dial
- armadillo c++
- cython
- MSVC++
- ctypes
- 볼륨 조절
- C++
- tensorflow
- Visual C++
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함