Search
 
SCRIPT & CODE EXAMPLE
 

CPP

error when using 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 :: c++ localtime unsafe 
Cpp :: c++ stoi binary negative number string to decimal 
Cpp :: define for loop c++ 
Cpp :: c to assembly mips converter 
Cpp :: static member fn , instance 
Cpp :: is obje file binary?? 
Cpp :: vowel and consonant program in c++ using if else 
Cpp :: viewlist exaple win32 
Cpp :: ternary operator rsut 
Cpp :: logisch nicht 
Cpp :: how to fix in c++ "cannot open imgui.h" 
Cpp :: c++ vector allocator example 
Cpp :: Qt asynchronous HTTP request 
Cpp :: facade pattern C++ code 
Cpp :: primtiive calculator in c++ 
Cpp :: KUNG FU HUSTLE 
Cpp :: c++ freecodecamp course 10 hours youtube 
Cpp :: set precision on floating numbers 
Cpp :: i++ and++i 
Cpp :: what is require to run min max function on linux in cpp 
Cpp :: C++ check if thread is joinable 
Cpp :: Calcular el número mayor y menor C++ 
Cpp :: 271533778232847 
Cpp :: c++ insert vector into vector 
Cpp :: type defination in C++ 
Cpp :: simplest code for stack implementation in c++ 
Cpp :: vector übergeben c++ 
Cpp :: Arduino Access Point ESP8266 
Cpp :: vprintf 
Cpp :: how to make a goto area in c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =