Skip to main content

C++ of the Day #3 - Multiply inherit from classes with conflicting function names

Question

역시 C++ 뉴스그룹에 올라온 것으로 찾아보면 자주 반복되어 온 질문중의 하나입니다. ((comp.lang.c++.moderated:Multiply inherit from classes with conflicting function names))
struct A {
  virtual long fun() = 0;
};
struct B {
  virtual bool fun() = 0;
};
struct Unfortunate : public A, public B {
  ???
}; 
질문을 요약해보면...
  1. ???부분을 합법적인 코드를 가지고 구현할 수 있는가?
  2. 물론 두 fun 함수 모두 각자의 구현을 가져야 한다.

Answer

워낙 많이 다루어진 질문이라 바로 답으로 넘어갑니다. 여기서 답이라는 것은 일반적으로 이런 방법을 사용한다는 뜻입니다.
다음과 같이 중간 계층의 클래스를 추가하여 각 함수에 고유의 이름을 줌으로써 각각의 함수 구현이 가능합니다.
struct Abis : A {
  virtual long funA() = 0;
  virtual long fun() { return funA(); }
};
struct Bbis : B {
  virtual bool funB() = 0;
  virtual bool fun() { return funB(); }
};
struct Unfortunate : Abis, Bbis {
  virtual long funA() { ... }
  virtual bool funB() { ... }
}; 

위의 코드를 사용하여 다음과 같이 사용할 수 있습니다.
Unfortunate u;
u.funA();
u.funB();
A* a = &u;
B* b = &u;
a->fun(); // call long Unfortunate::funA()
b->fun(); // call bool Unfortunate::funB()


2006-06-05 추가

관련된 읽은만한 글이 있어 추가합니다. 이제는 없어진 Herb와 Jim의 추억의 Conversation 컬럼이군요. - Adaptation

Comments

Popular posts from this blog

CodeHighlighter plugin test page.

This post is for testing CodeHighlighter plugin which uses GeSHi as a fontifier engine. ((Those code blocks are acquired from Google Code Search .)) ((For more supported languages, go CodeHighlighter plugin or GeSHi homepage.)) C++ (<pre lang="cpp" lineno="1">) class nsScannerBufferList { public: /** * Buffer objects are directly followed by a data segment. The start * of the data segment is determined by increment the |this| pointer * by 1 unit. */ class Buffer : public PRCList { public: Buffer() { ++index_; } PHP (<pre lang="php" lineno="4">) for ($i = 0; $i $value = ord( $utf8_string[ $i ] ); if ( $value < 128 ) { // ASCII $unicode .= chr($value); } else { if ( count( $values ) == 0 ) { $num_octets = ( $value } $values[] = $value; Lisp (<pre lang="lisp">) ;;; Assignment (define-caller-pattern setq ((:star var fo...

Textiler plugin test page

This post is for testing Textiler plugin . This plugin uses Textile engine (version 2.0.0). The sample text is come from Textile test page. (Note that the result will be vary according to your CSS options.) Supported wiki syntax Rendering result h2{color:green}. This is a title h3. This is a subhead p{color:red}. This is some text of dubious character. Isn't the use of "quotes" just lazy writing -- and theft of 'intellectual property' besides? I think the time has come to see a block quote. bq[fr]. This is a block quote. I'll admit it's not the most exciting block quote ever devised. Simple list: #{color:blue} one # two # three Multi-level list: # one ## aye ## bee ## see # two ## x ## y # three Mixed list: * Point one * Point two ## Step 1 ## Step 2 ## Step 3 * Point three ** Sub point 1 ** Sub point 2 Well, that went well. How about we insert an <a href="/" title="watch out">old-fashioned hypertext link</a>? Will the quo...

BookCover plugin test page

This post is for testing BookCover plugin . (( The previous test page has some comments from users.)) The 'Books Reading' section in the sidebar is also using this plugin. [ bookcover:1932394613] [bookcover:1932394613] [ bookcover:1590598385(Smart and Gets Things Done)] [bookcover:1590598385(Smart and Gets Things Done)] [ bookcover:1591841380(Wikinomics : How Mass Collaboration Changes Everything)] [bookcover:1591841380(Wikinomics : How Mass Collaboration Changes Everything)] [ bookcover:8995856408] [bookcover:8995856408] [ bookcover:8831793721] [bookcover:8831793721] [ bookcover:9788831793728] [bookcover:9788831793728]