Search
 
SCRIPT & CODE EXAMPLE
 

CPP

cannot access base class members

template<typename T>
class B {
public:
  void f() { }  // Member of class B<T>
};
template<typename T>
class D : public B<T> {
public:
  void g()
  {
    f();  // Bad (even though some compilers erroneously (temporarily?) accept it)
  }
};

/*************
Q:
Why am I getting errors 
when my template-derived-class uses a member it inherits from its template-base-class?

A:
Within D<T>::g(), the name f does not depend on template parameter T, 
so f is known as a nondependent name. 
On the other hand, B<T> is dependent on template parameter T 
so B<T> is called a dependent name.

Here’s the rule: 
the compiler does not look in dependent base classes (like B<T>) 
when looking up nondependent names (like f).

This doesn’t mean that inheritance doesn’t work. 
Class D<int> is still derived from class B<int>, 
the compiler still lets you implicitly do the is-a conversions (e.g., D<int>* to B<int>*), 
dynamic binding still works when virtual functions are invoked, etc. 
But there is an issue about how names are looked up.

Workarounds:
>>
Change the call from f() to this->f(). 
Since this is always implicitly dependent in a template, 
this->f is dependent and the lookup is therefore deferred 
until the template is actually instantiated, 
at which point all base classes are considered.
>>
Insert using B<T>::f; just prior to calling f().
>>
Change the call from f() to B<T>::f(). 
Note however that this might not give you what you want if f() is virtual, 
since it inhibits the virtual dispatch mechanism.

*************/
Comment

PREVIOUS NEXT
Code Example
Cpp :: For auto map C 
Cpp :: left margin c++ 
Cpp :: set keybinding for compiling c++ program in neovim 
Cpp :: determining a string is subsequence of another 
Cpp :: c++ find unused class methods 
Cpp :: how to refresh multiple command lines in C++ stream 
Cpp :: can map return a value to a variable in c++ 
Cpp :: 1/2(-3-3)(-3+4) 
Cpp :: high school hacking competition 
Cpp :: Boats to Save People leetcode solution in c++ 
Cpp :: fasdf 
Cpp :: Reading package lists... Done Building dependency tree Reading state information... Done mysql-server is already the newest version (5.7.36-0ubuntu0.18.04.1). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 
Cpp :: c++ create a vecto 
Cpp :: generate consecutive numbers at compile time 
Cpp :: Stream Overloading 
Cpp :: lnk2001 unresolved external symbol __imp_PlaySoundA 
Cpp :: c + + to c converter 
Cpp :: set app icon qt 
Cpp :: In-range Adder 
Cpp :: C is widely used for systems-level software and embedded systems development. 
Cpp :: construct string with repeat limit 
Cpp :: rotateArray 
Cpp :: std::string(size_t , char ) constructor: 
Cpp :: c++ coding questions for interview 
Cpp :: operator overloading 
Cpp :: c++ multi-dimensional arrays 
Cpp :: palindrome string 
Cpp :: what does | mean in c++ 
Cpp :: how to get part from the vector cpp 
C :: how to slow voice speed in pyttsx3 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =