C++ 다중 상속 관계에서 자식 클래스? 함수 호출하는 법??

jic5760의 이미지

안녕하세요.

class A {
public:
 void func_a()
 {}
}
 
class B {
 public:
 virtual void func_a() = 0;
 
 void func_b()
 {
  func_a();
 }
}
 
class Main : public A, public B
{
 
}

제가 사용하고 싶은 방식?은 이렇습니다.
그런데
error C2259: 'Main' : 추상 클래스를 인스턴스화할 수 없습니다.
이렇게 오류가 뜨네요...

이런식으로 사용할 수는 없나요?

twinwings의 이미지

Main이 class B를 상속받는데, 순수가상함수를 선언해놓고 그 어느곳에서도 구현하지 않았으니 당연히 instance를 생성할 수 없지요.

 의 이미지

class Main는 서로 아무 관련도 없는 두 개의 func_a 멤버 함수를 갖는 셈이거든요.

C++에서 그런 경우를 허용한다는 게 사용자 입장에서 편리할 때도 불편할 때도 있습니다만, 솔직히 이 모든 걸 알아야 한다는 게 너무 귀찮아서 솔직히 웬만하면 이런 식으로 이름을 중복시키고 싶지 않아요.

어쨌든 요점은 두 개의 멤버 함수가 독립적으로 존재한다는 것이고, 이름이 같다고 해서 한 쪽이 다른 쪽을 자동으로 오버라이드 해 주지는 않습니다. 결국 B::func_a는 순수 가상 함수로 남아 있으니 class Main의 인스턴스를 만들 수 있을 리가 없죠.

정 원한다면 이런 식으로 해결할 수도 있는 모양입니다.

#include <iostream>
using namespace std;
 
class A {
public:
	void func_a()
	{}
};
 
class B {
public:
	virtual void func_a() = 0;
	void func_b()
	{
		func_a();
	}
};
 
class Main : public A, public B
{
public:
	virtual void func_a()
	{
		A::func_a();
	}
};
 
int main() {
	Main m;
	m.func_a();
	m.func_b();
	return 0;
}

다중상속에서 같은 이름의 멤버함수 여러 개를 상속받았는데 이를 오버라이드할 시 어떤 것이 선택되는지에 대한 규칙이 표준에 아마 있을 법도 한데, 피곤해서 별로 찾아보고 싶은 생각은 안 드는군요.

그런데 이 코드의 경우 B::func_a가 오버라이드 되지 않으면 컴파일조차 안 될 테니, 최소한 원하는 대로 되었다는 점은 알 수 있겠습니다.

jic5760의 이미지

func_a()함수는 실제로 다중상속관계에 있기도 하구.. 복잡한데..
바로 B라는 클래스를 상속받아서 사용할 수 있게 하고 싶었는데 안되나보네요..ㅠㅠ
답변 감사합니다~^^

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.