inheritance with c++ templates [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inheritance and templates in C++ - why are methods invisible?
Why can't I use variable of parent class that is template class?
I'm trying to use template with inheritance and try to
access public member of parent class from child class. But this is not
compiling with g++.
template<class T>
class A
{
public:
A<T>* t;
};
template<class E>
class D:public A<E>
{
public:
D()
{
t = 0;
}
};
int main()
{
D<int> d;
return 0;
}
compile error:
g++ Test.C Test.C: In constructor D<E>::D():
Test.C:27: error: 't' was not declared in this scope

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;
}

Sometimes class members are not inherited by subclasses [duplicate]

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

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(){}