Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived template-class access to base-class member-data

This question is a furtherance of the one asked in this thread.

Using the following class definitions:

template <class T> class Foo {  public:     Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg)     {         /* do something for foo */     }     T Foo_T;        // either a TypeA or a TypeB - TBD     foo_arg_t _foo_arg; };  template <class T> class Bar : public Foo<T> { public:     Bar (const foo_arg_t bar_arg, const a_arg_t a_arg)     : Foo<T>(bar_arg)   // base-class initializer     {          Foo<T>::Foo_T = T(a_arg);     }      Bar (const foo_arg_t bar_arg, const b_arg_t b_arg)     : Foo<T>(bar_arg)     {         Foo<T>::Foo_T = T(b_arg);     }      void BarFunc ();  };  template <class T> void Bar<T>::BarFunc () {     std::cout << _foo_arg << std::endl;   // This doesn't work - compiler error is: error: ‘_foo_arg’ was not declared in this scope     std::cout << Bar<T>::_foo_arg << std::endl;   // This works! } 

When accessing the members of the template-class's base-class, it seems like I must always explicitly qualify the members using the template-style syntax of Bar<T>::_foo_arg. Is there a way to avoid this? Can a 'using' statement/directive come into play in a template class method to simplify the code?

Edit:

The scope issue is resolved by qualifying the variable with this-> syntax.

like image 885
Shamster Avatar asked Jul 13 '09 17:07

Shamster


People also ask

How do you access base class members in a derived class?

You can use both a structure and a class as base classes in the base list of a derived class declaration: If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private .

Can derived class access private members of base class?

Derived class can not access the private members of it's base class. No type of inheritance allows access to private members.

Does base class have access to derived class?

A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

Can derived classes access public members?

As a quick refresher, public members can be accessed by anybody. Private members can only be accessed by member functions of the same class or friends. This means derived classes can not access private members of the base class directly! This is pretty straightforward, and you should be quite used to it by now.


2 Answers

You can use this-> to make clear that you are referring to a member of the class:

void Bar<T>::BarFunc () {     std::cout << this->_foo_arg << std::endl; } 

Alternatively you can also use "using" in the method:

void Bar<T>::BarFunc () {     using Bar<T>::_foo_arg;             // Might not work in g++, IIRC     std::cout << _foo_arg << std::endl; } 

This makes it clear to the compiler that the member name depends on the template parameters so that it searches for the definition of that name in the right places. For more information also see this entry in the C++ Faq Lite.

like image 173
sth Avatar answered Sep 23 '22 07:09

sth


Here the base class is not a nondependent base class( which means one with a complete type that can be determined without knowing the template arguments), and _foo_arg is a nondependent name. Standard C++ says that nondependent names are not looked up in dependent base classes.

To correct the code, it suffices to make the name _foo_arg dependent because dependent names can be looked up only at the time of instantiation, and at that time the exact base specialization that must be explored will be known. For example:

// solution#1 std::cout << this->_foo_arg << std::endl; 

An alternative consists in introducing a dependency using a qualified name:

// solution#2 std::cout << Foo<T>::_foo_arg << std::endl; 

Care must be taken with this solution, because if the unqualified nondependent name is used to form a virtual function call, then the qualification inhibits the virtual call mechanism and the meaning of the program changes.

And you can bring a name from a dependent base class in the derived class once by using:

// solution#3 template <class T> class Bar : public Foo<T> { public:     ...     void BarFunc (); private:     using Foo<T>::_foo_arg; };  template <class T> void Bar<T>::BarFunc () {     std::cout << _foo_arg << std::endl;   // works } 
like image 34
songyuanyao Avatar answered Sep 19 '22 07:09

songyuanyao