S
Steven T. Hatton
In the following code, the class B will not compile if the function da is
uncommented. I took exactly the same code, and turned it into templates.
Never used the template parameter in anything but the baseclass name in the
derived class, and the code compiles without any complaints about protected
access. Why? What is different about the templated code?
class A{
public:
A(const int& x_=0): x(x_){}
protected:
int x;
};
class B: public A{
public:
B(const int& x_=0): A(x_){}
/*
B& da(const int& da_)
{
this->a.x += da_; // error: protected access
return *this;
}
*/
protected:
A a;
};
/*Same as above but with templates*/
template<typename T>
class AT{
public:
AT(const int& x_=0): x(x_){}
protected:
int x;
};
template<typename T>
class BT: public AT<T>{
public:
BT(const int& x_=0): AT<T>(x_){}
BT& da(const int& da_)
{
this->a.x += da_; // works fine
return *this;
}
protected:
AT<T> a;
};
uncommented. I took exactly the same code, and turned it into templates.
Never used the template parameter in anything but the baseclass name in the
derived class, and the code compiles without any complaints about protected
access. Why? What is different about the templated code?
class A{
public:
A(const int& x_=0): x(x_){}
protected:
int x;
};
class B: public A{
public:
B(const int& x_=0): A(x_){}
/*
B& da(const int& da_)
{
this->a.x += da_; // error: protected access
return *this;
}
*/
protected:
A a;
};
/*Same as above but with templates*/
template<typename T>
class AT{
public:
AT(const int& x_=0): x(x_){}
protected:
int x;
};
template<typename T>
class BT: public AT<T>{
public:
BT(const int& x_=0): AT<T>(x_){}
BT& da(const int& da_)
{
this->a.x += da_; // works fine
return *this;
}
protected:
AT<T> a;
};