2

I have a template base class.Lets say.

template<class KeyF>
class Base 
{
  private:
   int member1;
   char member2;
   ....
};

I derived another class from above class.

template<class KeyF>
class Derived : public Base<KeyF>
{
  public:
  void func1() {
    <accessing member1/member2>
  }

  ....
};

Above code doesn't compile in gcc. saying that member1 is not a member of Derived. But it is already derived from a Base Class, then why can't it access its member?

Dharmendra
  • 384
  • 1
  • 5
  • 22
  • 2
    `member1` and `member2` should be declared as `protected` or better you should provide protected getters/setters for them. – xaizek Sep 20 '12 at 14:22
  • 2
    xaizek is correct, but also see [this question](http://stackoverflow.com/questions/1120833/derived-template-class-access-to-base-class-member-data) - you need to qualify those accesses with `this->`. – DCoder Sep 20 '12 at 14:25

4 Answers4

4

You need to prefix base member names with this-> or Base<KeyF>::, or add a using declaration to the class to unhide them. Their names are dependent names, and they are hidden.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3

Members in Base are private. You cannot access private members of class, outside of this class (except friend). Make them protected, or make protected getters.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Public getters is a poor suggestion, as it breaks encapsulation. – David Rodríguez - dribeas Sep 20 '12 at 14:57
  • @DavidRodríguez-dribeas getters breaks encapsulation? i don't say setters, i say getters. – ForEveR Sep 20 '12 at 15:06
  • 1
    @ForEveR: Yes, they do. They leak to all other code the implementation details of your type. To help in the argument, I must say that I commonly use two terms: property and getter to mean different things. A 'property' is a feature of the object in the domain, while a 'getter' is a function provide access to the internals (i.e. 'size()' in a vector is a property of the type, which might or not be retrieving a member). Properties don't break encapsulation, as they are features of the object, but getters (not properties, but functions that return internals) break encapsulation. [...] – David Rodríguez - dribeas Sep 20 '12 at 16:02
  • 1
    [...] If the member is protected, then it is a detail of the internal interface between the base and derived types, not a public property in the domain. Making it public increases coupling for the rest of the code. You are leaking details of your implementation to all other code. – David Rodríguez - dribeas Sep 20 '12 at 16:03
1

Did you try protected? Been a bit since I was deep into C++...

0

I think two changes needed to solve the issue:

  1. In base class, define the member as "protected" instead of "private" to be accessible in the derived class.

  2. In derived class, add the base class name ahead of the protected member. In this case, it should look like "Base<typename>::member1".

Using C++17 standard in my case, the issue was resolved. Hope this is helpful. Thanks to Kerrek SB for the info.