| Ali89 (40) | |||||
|
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:
There is a protected data member (bit_table_) of class "bloom_filter" which I want to have access to it using the following line:
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. | |||||
|
|
|||||
| Zeillinger (121) | |
| 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. | |
|
|
|
| Ali89 (40) | |
|
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. | |
|
|
|
| Mathhead200 (951) | ||||
| ||||
|
|
||||
| Ali89 (40) | |||
|
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 ?
Best Regards, Ali. | |||
|
|
|||
| Mathhead200 (951) | |
|
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
|
|
| Ali89 (40) | |
| Thank you very much :-). | |
|
|
|