2013-05-09 2 views
6

는 Heres는 내 코드 :파생 클래스에서 기본 클래스에서 보호 된 멤버에 액세스 할 수 없습니다

#include <iostream> 
#include <cmath> 
#include <sstream> 
using namespace std; 

class root 
{ 
    protected : 

      int size; 
      double *array; 

    public : 

     virtual ~root() {} 
     virtual root* add(const root&) = 0; 
     virtual root* sub(const root&) = 0; 
     virtual istream& in(istream&, root&) = 0; 
     virtual int getSize() const = 0; 
     virtual void setSize(int); 
}; 

class aa: public root 
{ 

    public : 

     aa(); 
     aa(int); 
     aa(const aa&); 
     root* add(const root& a); 
     root* sub(const root& a); 
     istream& in(istream&, root&){} 
     int getSize() const; 
     void setSize(int); 
}; 

class bb: public root 
{ 
public: 
    bb() { } 
    bb(const bb& b) { } 
    root* add(const root& a); 
    root* sub(const root& a); 
    istream& in(istream&, root&){} 
    int getSize() const{} 
    void setSize(int){} 
}; 

aa::aa() 
{ 
    size = 0; 
    array = NULL; 
} 

aa::aa(int nsize) 
{ 
    size = nsize; 
    array = new double[size+1]; 
    for(int i=0; i<size; i++) 
     array[i] = 0; 
} 

root* aa::add(const root& a) 
{ 
    for (int i=0; i<a.size; i++) 
     array[i] += a.array[i]; 
    return *this; 
} 

root* aa::sub(const root& a) 
{ 
} 

int aa::getSize() const 
{ 
    return size; 
} 

void aa::setSize(int nsize) 
{ 
    size = nsize; 
    array = new double[size+1]; 
    for(int i=0; i<size; i++) 
     array[i] = 0; 
} 

root* bb::add(const root& a) 
{ 
    return new bb(); 
} 

root* bb::sub(const root& a) 
{ 

} 

int main(int argc, char **argv) 
{ 
} 

내가 size에 액세스하려는 또는 파생 클래스의 array, 난 그냥 내 컴파일러는 캔트 말한다 때문에 :

/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root* aa::add(const root&)’:| 
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: ‘int root::size’ is protected| 
/home/brian/Desktop/Temp/Untitled2.cpp|66|error: within this context| 
/home/brian/Desktop/Temp/Untitled2.cpp|11|error: ‘double* root::array’ is protected| 
/home/brian/Desktop/Temp/Untitled2.cpp|67|error: within this context| 
/home/brian/Desktop/Temp/Untitled2.cpp|68|error: cannot convert ‘aa’ to ‘root*’ in return| 
||=== Build finished: 5 errors, 0 warnings ===| 

보호 된 멤버가 파생 클래스에서 비공개 인 것을 읽었으므로 괜찮은 것으로 보이지만 그렇지 않습니다. 이 문제를 해결하는 방법?

답변

6

보호 된 멤버는 파생 클래스에서 비공개이므로 괜찮은 것으로 보이지만 그렇지 않습니다.

protected 데이터 부재 기본 클래스 A로부터 상속 때문에 (이 경우, aa) 파생 클래스 B 그래피 (root이 경우)되지 않고 것이면가 액세스됨에 따라 를 접근 유형이 B (aa) 인 객체입니다. 여기서, 사용자가 입력 A의 목적 (root)을 통해 액세스된다 : 전술 한 것 이외의

추가적인 액세스 확인 : 문단 C++ 11 표준 11.4/1 인분

root* aa::add(const root& a) 
{ 
    for (int i=0; i<a.size; i++) 
    //    ^^^^^^ 
    //    Accessing size on an object of type `root`, not `aa`! 
     array[i] += a.array[i]; 
    return *this; 
} 

non-static data 멤버 또는 비 정적 멤버 함수가 명명 클래스 (11.2)의 보호 된 멤버 일 때 11 절의 앞서 설명한 바와 같이 으로 설명한 것처럼 참조가 친구 또는 일부 클래스 C의 멤버에서 발생하기 때문에 보호 된 멤버에 대한 액세스가 허용됩니다. 액세스가 구성원 (5.3.1)에 대한 포인터를 형성하는 것이라면 중첩 된 이름 지정자는 C 또는 C에서 파생 된 클래스를 나타냅니다. 다른 모든 액세스에는 (암시적일 수도 있음) 객체 표현 (5.2.5) . [실시 예 :

class B { 
protected: 
    int i; 
    static int j; 
}; 
class D1 : public B { 
}; 
class D2 : public B { 
    // ... 
    void mem(B*,D1*); 
}; 

void D2::mem(B* pb, D1* p1) { 
    pb->i = 1; // ill-formed 
    p1->i = 2; // ill-formed 
    // ... 
    i = 3; // OK (access through this) 
    B::i = 4; // OK (access through this, qualification ignored) 
    j = 5; // OK (because j refers to static member) 
    B::j = 6; // OK (because B::j refers to static member) 
} 

- 엔드 예이 경우, 오브젝트의 발현 클래스 C 또는 C.에서 파생 된 클래스이어야한다

이를 해결하기 위해, 공공 세터/게터를 제공해야합니다.

for (int i=0; i<a.size; i++) 
//    ^^^^^^ 

당신이 쓸 수 : 마찬가지로

for (int i=0; i<a.getSize(); i++) 
//    ^^^^^^^^^^^ 

, 당신은 N- 값을/얻고 설정하는 기능을 제공해야합니다 당신은 이미, 그래서 대신이 글을 쓰는의 getSize() 기능을 가지고 array의 번째 요소, 당신은 쓸 수 있도록 :

array[i] += a.get_at(i); 

공지 사항, +=의 왼쪽에있는 표현은 OK입니다, 012,321,003 때문에은 this을 통해 액세스됩니다 (C++ 11 표준의 위 예제 참조).

+0

감사합니다. 그럼,이 문제를 해결할 방법이 있습니까? –

+2

@BrianBrown :'root'에'size()'라고 불리는'public' getter 함수를 사용할 수 있습니까? –

+0

''root''에서 이미 정의했지만 순수 가상 (그것은 잘못된 것입니까?). 그러나 배열은 어때? –

관련 문제