Problems with accessing to protected member

Hello,

I have declared a vector of objects. The class which I have declared a vector of it's objects has a protected data member. I need to access that protected data member using dot operator but I get errors !. My vector is declared as follows:

 
static std::vector<bloom_filter> relayBloomFilter (10, bloom_filter (SOURCE_RECEIVE_FILTER_SIZE, DPFP_FOR_SOURCE_RECEIVE_FILTER, RANDOM_SEED));


There is a protected data member (bit_table_) of class "bloom_filter" which I want to have access to it using the following line:

 
unsigned char* b_t = relayBloomFilter[1].bit_table_;


and finally the errors are:

../scratch/bloom_filter.hpp:408: error: ‘unsigned char* bloom_filter::bit_table_’ is protected
../scratch/BloomFilter02.cc:561: error: within this context

As I have read in C++ tutorial, The objects of a class can access protected data members using dot operator. So why I cannot do it in this case???. Any help is really appreciated :-).

Ali.
Protected members are only accessible from within the class itself, from a derived class or from a friend function/class. You can not access it using the dot operator if you aren't in one of those.
Thank you in advance :-). I believe your response was adequate but I will be indeed thankful if you help me with these two problems:
1- As you have seen in my first post, "bit_table_" is a protected member of class "bloom_filter"(I have downloaded a .hpp file which has implemented the class "bloom_filter" and in that header file "bit_table_" is declared as a protected member of the class "bloom_filter"). Then I have instantiated a vector of objects of class "bloom_filter" and want to access the protected member (bit_table_) using the dot operator..If "bit_table_" isn't accessible in an object of class "bloom_filter" using the dot operator, so how can I access it?. would you please help me??. I am really confused..

2-Assume I want to implement a class myself, how can I find out it is better to declare a particular member as a protected member of my class??

Many thanks again. my apologies for my long or maybe iterated questions..
Ali.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>

using namespace std;


class Base {
 private:
	int a;
 protected:
	int b;
 public:
	int c;

	int getA() { return a; }
	int getB() { return b; }
	void setA(int a) { this->a = a; }
	void setB(int b) { this->b = b; }

	void example1() {
		cout << a << ", " //a is visible
			<< b << ", " //b is visible
			<< c << endl; //c is visible
	}
};

class Sub : public Base {
 public:
	void example2() {
		// cout << a << ", " //a is hidden
		cout << b << ", " //b is visible
			<< c << endl; //c is visible
	}
};

void example3(Base *base) {
	// cout << base->a << ", " //a is hidden
	// cout << base->b << ", " //b is hidden
	cout << base->c << endl; //c is visible
}


int main() {
	Base *x = new Sub,
		*y = new Sub,
		*z = new Sub;

	x->setA( 2 ); //x->a is not public, so we use the appropriate mutator method
	x->setB( 3 ); //same thing for x->b 
	x->c = 5; //x->c is public, so we can access it directly

	y->setA( 10 );
	y->setB( 20 );
	y->c = 30;

	z->setA( 1024 );
	z->setB( 1048576 );
	z->c = 1073741824;

	x->example1(); //defined in Base, will display a, b, and c
	y->example2(); //defined in Sub, will display only b, and c
	example3( z ); //defined as a global function, will display only c

	delete x;
	delete y;
	delete z;

	return 0;
}
2, 3, 5
20, 30
1073741824
Dear Mathhead200,

Many thanks indeed :-). You gave me good insight about the topic.
The following code is copied from the C++ tutorial. In the following example "bobby" is a pointer to integer (int * bobby) and the type which is written after "new" is integer too. But in the lines 43, 44 and 45 of your example, there are two different types (the type which the pointer points to and the type which comes after "new" are different). Definitely, you have had a very nice reason. Would you please tell me about your reason ?

1
2
3
4
5
6
pointer = new type
pointer = new type [number_of_elements]

// example
int * bobby;
bobby = new int [5];


Best Regards,
Ali.
Tutorial reference: http://cplusplus.com/doc/tutorial/polymorphism

Actually it wasn't need here. Line 44 could have been replaced with Sub *x = new Sub, and arguably should have been to avoid confusion. However since class Sub extends from class Base you can think of a Sub* as being a Base* also (i.e. A tiger is an animal.)

Actually looking back on it in order for line 61 to compile you would need to add the following line to the class Base: virtual void example2() = 0; However, then you might as well make all the methods in Base virtual ;)

Sorry about that!
Last edited on
Thank you very much :-).
Topic archived. No new replies allowed.