I’m trying to make the following bit of code to work…
#include <list>
template <typename T>
class container{
public:
virtual T func_x(){
T temp;
//do stuff with list<t> test
return temp;
}
private:
std::list<T> test;
};
template <typename T>
class container2 : public container<T>{
public:
virtual T func_x(){
T temp;
//do different stuff with list<T> test
return temp;
}
};
What I want to be able to do is declare
container<T> x;
container2<T> y;
And be able to have y have access to all the public functions of x, except that it behaves differently for func_x.
The problem I have now is that func_x in class container2 cannot use;
std::list<T> test;
I have even tried making class container completely public. Still no dice. Can this be done?
Thanks!
Members are by default
privatefor classes:means that
func_xisprivate, since no modifier is specified.You need to explicitly declare
func_xas public, as you have forclass container.“Just because it’s
publicin the base class doesn’t mean it’s automatically that way for the derived”.EDIT:
If you want base class members to be accessible in derived classes, you must declare them either
protectedorpublic. So, to answer your follow-up question, changeto
Also, in the future, don’t edit the question to ask a new one. You should create a new question to deal with the new issue. It can be misleading for others who see answers that no longer apply to the new question.