inheritance problem

Let's say i have two classes, A and B.
1
2
3
4
5
6
7
8
9
10
class A{
protected:
    _member;

...
}

class B : public A{
...
}

It seems that i cannot access A's data member _member inside class B, but why? i think the protected data member should be seen by the subclass.
.
Last edited on
once you set public for your derived class
you only can access the public data member of class A

Wrong. http://www.cplusplus.com/doc/tutorial/inheritance/ scroll down to "Inheritance between classes"

@zhaorenbo
Does the compiler give any error? Can you post the code where you try to access it?
@maeriden
Ok. here's my code.
LIstBase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  1 #ifndef _LISTBASE_H_
  2 #define _LISTBASE_H_
  3
  4 #include <string>
  5 using namespace std;
  6
  7 class ListBase   {
  8     protected:
  9         int _size;
 10
 11     public:
 12         ListBase() {
 13             _size = 0;
 14         }
 15
 16         bool isEmpty(){
 17             return (_size == 0);
 18         }
 19
 20         int getLength(){
 21             return _size;
 22         }
 23
 24         virtual bool insert(int index, const int& newItem) = 0;
 25
 26         virtual bool remove(int index) = 0;
 27
 28         virtual bool retrieve(int index, int& dataItem) = 0;
 29
 30         virtual string toString() = 0;
 31
 32 };
 33
 34 #endif 


And ListArray.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  1 #ifndef _LISTARRAY_H_
  2 #define _LISTARRAY_H_
  3
  4 #include "ListBase.h"
  5 #include <string>
  6 using namespace std;
  7
  8 const int MAX_LIST= 50;
  9
 10 class ArrList : public ListBase  {
 11     private:
 12         int _items[MAX_LIST];
 13
 14     public:
 15         ArrList() : ListBase(){}
 16
 17         ArrList(ListBase *listPtr){
 18             for (int i = 0; i < listPtr->_size; i++)
 19                 listPtr->retrieve(i + 1, _items[i]);
 20             _size = listPtr->_size;
 21         }
 22
 23         ArrList(ArrList &list){
 24             *this = list;
 25         }
 26
 27         bool insert(int index, const int& newItem);
 28
 29         bool remove(int index);
 30
 31         bool retrieve(int index, int& dataItem);
 32
 33         string toString();
 34
 35 };
 36
 37 #endif 

I argee with you. But the compiler shows
'_size' is protected in ListBase
. I've used public access qualifier, so the inherited members in ListArray should have the same access permissions as in ListBase. Can anyone explain why?
In lines 18 and 20 you're trying to access the '_size' of another instance of the class. Since you inherited from ListBase with public, '_size' is protected in ArrList too. Each instance has access only to its own private and protected members
Last edited on
@maeriden
No,
Each instance has access only to its own private and protected members
is wrong. If two objects are of the same class, then you can access one object inside the other object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct A { protected: int i = 0 ; } ;

struct B : A
{
    void foo() { i = 7 ; } // fine, B::i i inheried by B
    void bar( B& b ) { b.i = 7 ; } // fine, B::i i inheried by B

    void baz( A& a ) { a.i = 7 ; } // ***error, A::i i is not inheried by B

    struct C : A {} ;
    void foobar( C& c ) { c.i = 7 ; } // ***error, C::i

    void barbaz()
    {
        this->i = 7 ; // fine, B::i
        this->A::i = 7 ; // fine, B::i
        A* This = this ;
        This->i = 7 ; // ***error, A::i
    }
};
Mmm you're right. That was embarrassing, I assumed and failed.
My next guess is that you're passing a pointer to the base class, which is considered a different class and therefore negates the access to its private members
@zhaorenbo

ListBase is an abstract class - it has pure virtual functions, so one cannot create objects with this class. Did you create an object of type ArrList, and send a pointer to it to the function?

Edit Sending a pointer to the base class as an argument is OK because of polymorphism, but the pointer needs to come from a derived class.

Edit: Declaring a function to take a pointer to a base class as a parameter is OK, because of polymorphism, but the argument needs to be a pointer to the derived class.

So supplying a pointer to a derived class to a derived class constructor, is a bit weird. What are you wanting to do exactly?
Last edited on
Topic archived. No new replies allowed.