Re: Weird protected access problem.
* ??:
Hi, folks,
My g++ is of version 3.4.2, here is the code snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is B\n";
}
};
template<typename T>
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived<T> D() {
Base b;
return Derived<T>(b);
}
};
int main() {
Derived<int> obj;
obj.D();
return 1;
}
When I tried to build this program, an error arraised:
test.cpp:7: error: `Base::Base()' is protected
test.cpp:18: error: within this context
Any informative tips will be appreciated, thank you.
If C++ allowed you to freely access protected features of a class in a
derived class, then you could circumvent any "protected:" access
protection simply by deriving a class; in particular you could access
Base "protected:" features on instances of SomeoneElsesDerivedClass.
Therefore you can't.
In class Derived you can only access protected Base features on
instances of Derived, or on instances of classes derived from Derived,
and just for completeness, this has nothing to do with templating.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?