#include <iostream>
#include <string>
using namespace std;
class Person {
public:
Person() : name("Kim") {}
explicit Person(const string& name) : name(name) {}
void set_name(const string& name) { this->name = name; }
string get_name() const { return name; }
virtual void all_info() const {
cout << "[person] My name is " << name << endl;
}
private:
string name;
};
class Student : public Person {
public:
Student(const string& name, const string& passed) :
Person(name), passed(passed) {}
virtual void all_info() const override {
cout << "[student] My name is " << get_name() << endl;
cout << "I passed the following grades: " << passed << endl;
}
private:
string passed;
};
void Update(Student* student) {
}
void main() {
Student student("Kim", "good");
const Student& c_student = student;
// INFO: 함수 파라미터는 const가 없는데 const를 넘겨주면 오류 발생
Update(&c_student);
// INFO: OK, const가 명확하게 제거됨
Update(const_cast<Student*>(&c_student));
// INFO: 이것도 OK이지만, C 스타일 캐스트로 피하는 것이 좋다.
Update((Student*)(&c_student));
}
참조: http://egloos.zum.com/sweeper/v/1907485