[ Effective C++ ] 정리 모음집
" C++ 프로그래머의 필독서, 스콧 마이어스의 Effective C++ 를 읽고 내용 요약 / 정리 "
" 기본 클래스의 멤버에 대한 참조가 무효한지를 컴파일러가 진단하는 과정이 미리 들어가느냐, 나중에 들어가느냐가 이번 항목의 핵심! C++은 이른 진단을 선호하는 정책을 결정! "
- 파생 클래스 템플릿에서 기본 클래스 템플릿의 이름을 참조할 때는, "this->"를 접두사로 붙이거나 기본 클래스 한정문을 명시적으로 써 주는 것으로 해결하자!
class CompanyZ
{
public:
...
void sendEncrypted(const string& mgs);
...
};
[CompanZ 를 위한 MsgSender의 특수화 버전]
template<>
class MsgSender<CompanyZ>
{
public:
...
void sendSecret(const MsgInfo& info)
{ ... }
};
template<typename Company>
class LoggingMasgSender : public MsgSender<Company>
{
public:
...
void sendClearMsg(const MsgInfo& info)
{
"메세지 전송 전" 정보를 로그에 기록
sendClear(info);
"메세지 전송 후" 정보를 로그에 기록
}
...
};
MsgSender<CompanyZ>
이면 말이 되지 않는 코드이다 MsgSender<CompanyZ>
클래스에는 sendClear
함수가 없으므로..📢 위와 같은 이유로 C++ 컴파일러는 템플릿으로 만들어진 기본 클래스를 뒤져서 상속된 이름을 찾는 것을 거부한다!
template<typename Company>
class LoggingMsgSender : public MsgSender<Company>
{
public:
...
void sendClearMsg(const MsgInfo& info)
{
"메세지 전송 전" 정보를 로그에 기록
this->sendClear(info);
"메세지 전송 후" 정보를 로그에 기록
}
...
};
this->sendClear(info);
template<typename Company>
class LoggingMsgSender : public MsgSender<Company>
{
public:
...
using MsgSender<Company>::sendClear;
...
void sendClearMsg(const MsgInfo& info)
{
"메세지 전송 전" 정보를 로그에 기록
sendClear(info);
"메세지 전송 후" 정보를 로그에 기록
}
...
};
using MsgSender<Company>::sendClear;
template<typename Company>
class LoggingMsgSender : public MsgSender<Company>
{
public:
...
void sendClearMsg(const MsgInfo& info)
{
"메세지 전송 전" 정보를 로그에 기록
MsgSender<Company>::sendClear(info);
"메세지 전송 후" 정보를 로그에 기록
}
...
};
MsgSender<Company>::sendClear(info);
LoggingMsgSender<CompanyZ> zMsgSender;
MsgInfo msgData;
... // msgData에 정보를 채운다
zMsgSender.sendClearMsg(msgData); // 컴파일 되지 않는다, 에러!
기본 클래스가 MsgSender<CompanyZ>
라는 사실을 컴파일러가 알고 있음
sendClearMsg
함수가 호출하려고 하는 sendclear
함수는 MsgSender<CompanyZ>
클래스에 안 들어 있다는 사실도 컴파일러가 알아챈 후 이기 때문..