강의

멘토링

커뮤니티

Inflearn Community Q&A

rlatjrfo571111's profile image
rlatjrfo571111

asked

Following and Learning C++ with Hong Jeong-mo

9.5 Overloading Increment and Decrement Operators

전위 연산자 만들기 4분21초 기준 14번째 줄

Written on

·

253

0

++m_digit; 부분을 ++(*this); 로 바꾸면 작동이 안됩니다, 왜 안되는 건지 궁금합니다.

C++

Answer 2

0

rlatjrfo57님의 프로필 이미지
rlatjrfo57
Questioner

실행은 되는데, m_digit의 값이 ++m_digit; 할 때처럼 정상적으로 올라가지 않습니다.

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

class Digit
{
private:
	int m_digit;
public:
	Digit(int digit = 0) : m_digit(digit) {}

	Digit & operator ++ () //전위 연산자
	{
		//++(m_digit);
		++(*this);
		return *this;
	}

	Digit operator ++ (int) //후위 연산자
	{
		Digit temp(m_digit);
		++(*this);
		return temp;
	}

	friend ostream & operator << (ostream &out, const Digit &d)
	{
		out << d.m_digit;
		return out;
	}
};

int main()
{
	Digit d(5);

	cout << ++d << endl;
	cout << d << endl;
	cout << d++ << endl;
	cout << d << endl;

	return 0;
}

0

honglab님의 프로필 이미지
honglab
Instructor

실행이 되어야 하는데 안된다는 얘기시지요? 이상하네요. 실행 가능한 전체 코드를 올려주시면 제가 한 번 해보겠습니다.

rlatjrfo571111's profile image
rlatjrfo571111

asked

Ask a question