Re: Multi-level inheritance and accessing base protected member variables
Joseph Paterson je napisao:
Hi all,
I'm having some trouble with the following code (simplified to show
the problem)
class Counter
{
protected:
int m_counter;
}
template <class K, class V>
class IMap : public Counter
{
// Virtual function declarations
}
template <class K, class V>
class HashMap : public IMap<K, V>
{
// Implementation of the virtual functions declared in IMap
}
Now when I'm implementing the functions in the HashMap class, I can't
seem to access the m_counter member variable by just using m_counter
(I get an undeclared reference from GCC), but when I use this-
m_counter, then it works fine.
Could anybody help me out with this one?
Thanks,
Joseph Paterson.
If you exclude some errors (like missing ; at end of class or using
class instead of typename) I do not
see what is wrong, here is code that compiles without errors (on MINGW
and VC8):
class Counter
{
protected:
int m_counter;
};
template <typename K, typename V>
class IMap : public Counter
{
public:
IMap() {}
virtual void foo() = 0;
};
template <typename K, typename V>
class HashMap : public IMap<K, V>
{
public:
HashMap() {}
void foo() {
m_counter = 10;
}
} ;
int main()
{
HashMap<int, int> a;
a.foo();
}
P.S.
You can always test code on multiple compilers at http://dinkumware.com/exam/
..
Best,
Pasalic Zaharije