subclass에서 superclass의 멤버 변수의 일부를 사용 금지시킬 수 있나요?

inb612의 이미지

안녕하세요. C++이나 Java나 아무 OOP 언어에서요.
부모로부터 상속받은 멤버 변수 중 일부를 사용 금지할 수 있는 방법이 있나요?

제가 superclass를 하나 만들고 나서 상속받는 subclass를 여러 개 만들려고 합니다.

class superclass {
public:
int i, j;
};

class subclass1 : public superclass {
public:
...
}

class subclass2 : public superclass {
public:
...
}

이런 식으로요. 이 여러 개의 subclass 중에서 딱 한 개, subclass1에서만 i를 사용 금지시키는 방법이 없을까요?
subclass1에서는 i를 사용할 일이 없습니다.
그렇다고 superclass에서 i를 빼버리자니 제가 생각할 수 있는 방법은
1) 나머지 수많은 subclass2, subclass3, ...에 모두 i를 멤버 변수로 넣거나
2) superclass를 2개를 만들어야 하는데 (i만 가진 것과, i를 상속받아서 j를 추가한 것)
인데, 둘다 마음에 들지 않네요.

그나마 2)가 1)보다는 나아보이는데요.
하지만 나중에 디자인이 변경되어서 subclass1에서 i가 아닌 j를 사용금지하게 될 일도 있거든요.
그럴 때는 subclass1뿐만 아니라 superclass도 다 고쳐야해서 2)도 마음에 들지 않네요.

어떤 방법이 가능할까요?

HDNua의 이미지

1. 멤버 변수를 직접 이용하지 말고 멤버 변수에 대한 접근자를 정의하면 어떨까 합니다.

#include <iostream>
 
class Base {
	int _member1;
	int _member2;
public:
	Base(): _member1(0), _member2(0) {}
	int member1() const { return _member1; }
	int member2() const { return _member2; }
	void setMember1(int value) { _member1 = value; }
	void setMember2(int value) { _member2 = value; }
	void show() { std::cout<<_member1<<" : "<<_member2<<std::endl; }
};
class Base_Member1Unusable: public Base { // member1을 사용할 수 없는 클래스
	int member1() const { return 0; } // 해당 멤버에 대한 접근자를 private으로 변경
	void setMember1(int value) { }
};
class Child1: public Base_Member1Unusable {};
class Child2: public Base {};
 
int main(void) {
	Child1 child1;
	Child2 child2;
 
//	child1.setMember1(1);
	child1.setMember2(2);
	child1.show();
 
	child2.setMember1(1);
	child2.setMember2(2);
	child2.show();
 
	return 0;
}

다만 이렇게 만들면 Base 포인터로 해당 멤버를 가리키는 경우는 컴파일 중에는 검출하지 못합니다.
런타임에 dynamic_cast로야 겨우 가능할 것 같네요.

저는 이렇게 생각했습니다.

twinwings의 이미지

sub1 is not super.
sub2 is super.

입니다. 즉, 요구하시는 것은 상속의 기본 원칙(철학)을 어긴다고 생각되네요.

차라리

      su-super
      |     |
   sub1   super
            |
        sub2, sub3, sub4...

관계를 만드는건 어떤지요

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.