Let's look at this simple code sample including a base class and a class derived from Base, which needs the address of a base class member in its constructor.
#include <vector>
#include <inttypes.h>
#include <stdio.h>
class Base
{
protected:
std::vector<uint32_t> arr;
public:
Base(std::vector<uint32_t> arr_in): arr(arr_in) {}
};
class Derived: public Base
{
private:
uint32_t *parr;
public:
Derived(std::vector<uint32_t> arr_in): Base(arr_in)
{
parr = &arr[0];
}
uint32_t *get_parr();
};
uint32_t *Derived::get_parr(void)
{
return parr;
}
int main()
{
std::vector<uint32_t> myarr(3, 1);
Derived myderived(myarr);
printf("myderived.myarr adress = %p", myderived.get_parr());
}
Since the constructor of the derived class calls the base class constructor first and only then executes its code block, the members of the base class can already be accessed. So everything works fine.
Now I change the code sample so that my two classes are templates.
#include <vector>
#include <inttypes.h>
#include <stdio.h>
template<typename T>
class Base
{
protected:
std::vector<T> arr;
public:
Base(std::vector<T> arr_in): arr(arr_in) {}
};
template<typename T>
class Derived: public Base<T>
{
private:
T *parr;
public:
Derived(std::vector<T> arr_in): Base<T>(arr_in)
{
parr = &arr[0];
}
T *get_parr();
};
template<typename T>
T *Derived<T>::get_parr(void)
{
return parr;
}
int main()
{
std::vector<uint32_t> myarr(3, 1);
Derived<uint32_t> myderived(myarr);
printf("myderived.myarr adress = %p", myderived.get_parr() );
}
But this second sample gives me the following error message upon compiling:
class_temp.cpp: In constructor ‘Derived<T>::Derived(std::vector<T>)’:
class_temp.cpp:23:13: error: ‘arr’ was not declared in this scope
parr = &arr[0];
So why is it that in the second sample with template classes the derived class constructor doesn't know about the base class member? Or am I doing something wrong here?
Thank you.