Sometimes class members are not inherited by subclasses [duplicate] - c++

This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
Derived template-class access to base-class member-data
(3 answers)
Closed 5 years ago.
When we create a template class example1 which derives from another template class example2. The template class example1 does not inherit systematically of example2 members.
Consider the code bellow:
template<class T>
class example2 {
protected:
int a;
public:
int getA() const {return a;}
};
template<class T>
class example1 : public example2<T> {
T* p;
public:
void test() {
example2<T>::a = 8;
}
};
in the method test of the class example1, we have to use example2<T>:: operator in order to be able to access to the member a. Otherwise, the complier generates an error: 'a' was not declared in this scope.
I mean, why the complier does not recognize a in the class example1? Why we should specify example2<T>::a and not just a?
Thanks

Related

Why the incomplete type can be new from the base class with template technique [duplicate]

This question already has an answer here:
CRTP -- accessing incomplete type members
(1 answer)
Closed 11 months ago.
The following code will fail
class InComplte;
void f() {
new InComplte();
}
class InComplte {
};
Because it is newing an incomplete type.
But if template is used, the following code can compile
template<typename T>
class CComCoClass {
public:
static void create_di(T**p) {
*p = new T();
}
};
class Book: public CComCoClass<Book> {
public:
int i = 3;
};
The base class CComCoClass seems to be newing an incomplete type.
Why the Book can inherit a base class that creates it?
A member function of a class template is not evaluated when the class template is instantiated. The functions instantiation is delayed until it is called. create_di isn't called in the example, so it isn't evaluated.

Derived class cannot see base class member [duplicate]

This question already has answers here:
Derived template-class access to base-class member-data
(3 answers)
Closed 2 years ago.
What am I doing wrong in this code?
template <typename T>
class CLASS1
{
public:
T member;
};
template <typename T>
class CLASS2 : public CLASS1<T>
{public:
void func()
{
member = 4;
}
};
Visual Studio error:
member identifier not found
G++ error:
‘member’ was not declared in this scope
You need to specify where the name member comes from. In this case, it comes from the inherited class template CLASS1<T>, so you need to say:
void func()
{
CLASS1<T>::member = 4;
}
If you say this->member, then the compiler knows to look for names in the base classes as well. So you could do:
void func()
{
this->member = 4;
}

Is it possible to access a member inside the parent class body with CRTP? [duplicate]

This question already has answers here:
invalid use of incomplete type
(5 answers)
Closed 4 years ago.
Is there a way in C++11/14/17 to access a member of the subclass in the parent class when using CRTP?
template <typename T>
class A {
public:
using C = typename std::result_of<decltype(&T::next)(T)>::type;
};
class B : A<B> {
public:
int next() { ... };
};
This should result in A<B>::C and B::C being int.
No, I am afraid that is not possible. When A<B> is used as the base class, it must be instantiated (since a base class must be complete), but at that point B is still an incomplete type and thus its members cannot be accessed inside A<B>.

how to inherit a class with templates? [duplicate]

This question already has answers here:
Accessing inherited variable from templated parent class [duplicate]
(2 answers)
Why do I have to access template base class members through the this pointer?
(3 answers)
Closed 9 years ago.
Why would the following work just fine:
class a
{
public:
int n;
};
class b : public a
{
public:
b()
{
n = 1;
}
};
int main()
{
}
but this does not work:
template <class T>
class a
{
public:
int n;
};
template <class T>
class b : public a<T>
{
public:
b()
{
n = 1;
}
};
int main()
{
}
and gives the following error:
1.cpp: In constructor ‘b<T>::b()’:
1.cpp:14: error: ‘n’ was not declared in this scope
and how would one inherit a template class while being able to use its base members and keep the type generic?
You need to qualify it with "this" or with a "using" directive (or explicitly with the base class qualification).
In a nutshell: that variable is a nondependent type (non-dependent on the T of the template class), the compiler doesn't look into a dependent type (your a< T >) when looking for declarations for a nondependent type.
this->n, since "this" refers to a dependent class, works. The same with the other methods I listed.
References:
Faq here: http://www.parashift.com/c++-faq-lite/nondependent-name-lookup-members.html
Live example here: http://ideone.com/nsw4XJ

Templated class members in the base class, don't exist in the derived classes [duplicate]

This question already has answers here:
Name lookups in C++ templates
(3 answers)
Closed 9 years ago.
Observe the following program.
class Base{
protected:
int datum;
};
class D: public Base{
public:
int Get_Datum(){
return datum;
}
};
int main(){}
The base class holds a member variable. We've explicitly stated that the member variable is an integer. The derived class can inherit from the base class and access that member variable. This compiles and works as expected.
Now let's try the same thing but have the data member deduced at compile time.
template <typename Datum>
class Base{
protected:
Datum datum;
};
template <typename Datum>
class D: public Base<Datum>{
public:
int Get_Datum(){
return datum;
}
};
int main(){}
15:10: error: ‘datum’ was not declared in this scope
How do I edit this in order to get it to work the same way as the first example? Do I have to do something with the constructors?
Change Get_Datum to be:
int Get_Datum()
{
return this->datum;
}
For an explanation, read the answer here -> https://stackoverflow.com/a/10639312/986
Thank you. The possible duplicate link provided, answers my question.
The trick is to add "this->", turning the datum into a dependent name.
Solution:
template <typename Datum>
class Base{
protected:
Datum datum;
};
template <typename Datum>
class D: public Base<Datum>{
public:
int Get_Datum(){
return this->datum; //<- "this->" added
}
};
int main(){}