반응형
#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));
}
반응형
'컴퓨터 공학 > C++' 카테고리의 다른 글
[C++11] 초간단 시간 측정 클래스 (0) | 2019.11.14 |
---|---|
C++ 함수 내 람다 함수를 통해 코드 정리 (0) | 2019.11.14 |
C++ exit()와 quick_exit()의 차이점 (0) | 2019.05.24 |
c++ unordered_map 사용 예시 (0) | 2019.04.06 |
[C/C++] 윈도우즈 운영체제에서 맥 주소 가져오기 (0) | 2018.09.18 |