You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
File 클래스 안에 fileName이라는 멤버 변수가 존재한다면 IOFile 클래스에는 기본적으로 중복 생성된 fileName 멤버 변수를 가진다.
가상 기본 클래스
MI 마름모꼴을 해결방법은 가상 기본 클래스가 있다.
가상 기본 클래스로 삼을 클래스에 직접 연결된 파생 클래스에서 가상 상속을 사용하는것이다.
classFile{}; // 가상 기본 클래스classInputFile: virtual public File{};
classOutputFile: virtual public File{};
classIOFile: publicInputFile, publicOutputFile{};
비용이 늘어나고 성능이 저하된다.
초기화 및 대입연산의 복잡도가 커지는 단점이 있다.
가상 기본 클래스는 사용하지 않는 것이 좋다.
가상 기본 클래스를 쓰지 않으면 안 될 상황이라면, 데이터를 넣지 않도록 신경 쓴다.
다중 상속을 적법하게 쓸 수 있는 경우
인터페이스 클래스로부터 public 상속을 시키고 구현을 돕는 클래스로부터 private 상속을 시킨다.
// 용도에 따라 구현될 인터페이스classIPerson {
public:virtual~IPerson();
virtual string GetName() const = 0;
};
// IPerson 인터페이스를 구현하는 데 유용한 함수가 들어있는 클래스classPersonInfo {
public:explicitPersonInfo(int pid);
virtual~PersonInfo();
virtualconstchar* theName() const;
virtualconstchar* valueDelimOpen() const;
virtualconstchar* valueDelimClose() const;
};
classCPerson : publicIPerson, privatePersonInfo {
public:explicitCPerson(int pid) : PersonInfo(pid) {}
// IPerson 클래스의 순수 가상 함수를 파생 클래스에서 구현virtual string GetName() const
{
returnPersonInfo::theName();
}
private:// 상속된 가상 함수들도 재정의 버전 구현constchar* valueDelimOpen() const { return""; }
constchar* valueDelimClose() const { return""; }
};