나 보려고 만든거

c++ Explicit & Mutable

Realsung 2021. 1. 6. 08:35

explicit : 명백한, 명시적

mutable : 변하는, 변하기 쉬운

 

explicit으로 생성자가 복사 생성자의 형태로 호출 x

class내에서 explict CLASS(int a); 선언

ex) Class obj = 5; -> X

ex) Class obj(5) -> O

 

mutable

#include <iostream>

class A {
private:
	mutable int t; // mutable을 선언해서 const함수 안에서 멤버변수의 값을 변경할 수 있음
    // mutable을 선언안하면 DomSomeThing에서 오류뜸
public:
	A(int a);
	void DoSomeThing(int x) const;
	void print() const;
};

A::A(int a) : t(a) {}

void A::DoSomeThing(int x) const {
	t = x; // if modify t? in const function -> using mutable variable!
}

void A::print() const {
	std::cout << t << std::endl;
}

int main() {
	A a(10);
	a.DoSomeThing(11);
	a.print();
}