Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access protected member of a class in a derived class

i have an old codebase here, where they used protected member variables. Whether or not this is a good idea can be discussed. However, the code must have compiled fine with gcc3. I have a derived template class Bar that uses protected member x from class template Foo like so

template <class Something> class Foo {  
public:  
// stuff...  
protected:  
  some::type x;  
}

template <class Something> Bar : Foo<Something> {
public:
  void cleanup();
}

And in the method declaration of cleanup() there is something done with x

template <class Something> void Bar<Something>::cleanup() {
  doSomeThingCleanUpLike (x);
}

This does not work with gcc4, although it should have worked with gcc3. It works when I change it to

doSomeThingCleanUpLike (this->x);

Why is that the case?

like image 443
GeeF Avatar asked Oct 26 '09 12:10

GeeF


People also ask

Can a derived class access protected members of a parent class?

It cannot access protected members of instances of a parent class or cousin class. In your case, the Derived class can only access the b protected member of Derived instances, not that of Base instances. Changing the constructor to take a Derived instance will solve the problem.

Can a class access a protected member of a base instance?

A class can only access protected members of instances of this class or a derived class. It cannot access protected members of instances of a parent class or cousin class. In your case, the Derived class can only access the b protected member of Derived instances, not that of Base instances.

What is an example of a protected member in Java?

Example. A protected member of a base class is accessible in a derived class only if the access occurs through the derived class type. For example, consider the following code segment: class A { protected int x = 123; } class B : A { static void Main() { A a = new A(); B b = new B(); // Error CS1540,...

What is protected access in C++?

This page covers protected access. The protected keyword is also part of the protected internal and private protected access modifiers. A protected member is accessible within its class and by derived class instances. For a comparison of protected with the other access modifiers, see Accessibility Levels.


1 Answers

The expression x used in the derived class is, by the rules in the standard, not dependent on any template parameter of the derived class. Because of this, lookup happens in the context of the template definition and not at the point of use/instantiation. Even though the template base class of the template appears to be visible, because it is a template class the particular instantiation that might be used might involve specialized templates so the base class template definition cannot be used for name lookup.

By changing the expression to this->x you are making it a dependent expression (this in a class template always depends on the template parameters). This means that lookup will occur in the instantiation context at which point the base class is fully known and its members are visible.

like image 191
CB Bailey Avatar answered Oct 01 '22 08:10

CB Bailey