Multi-level inheritance and accessing base protected member variables

J

Joseph Paterson

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.
 
H

Hari

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-

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
 
S

Sumit Rajan

Joseph said:
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-

Could anybody help me out with this one?

How about using this->m_counter instead of "just using" m_counter? Does
that fix your problem?
 
S

Sumit Rajan

Hari said:
Joseph Paterson je napisao:
Path: uni-berlin.de!fu-berlin.de!postnews.google.com!q69g2000hsb.googlegroups.com!not-for-mail
From: Hari <[email protected]>
Newsgroups: comp.lang.c++
Subject: Re: Multi-level inheritance and accessing base protected member variables
Date: Thu, 28 Jun 2007 01:27:16 -0700
Organization: http://groups.google.com
Lines: 83
Message-ID: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: 80.65.75.8
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Trace: posting.google.com 1183019236 7114 127.0.0.1 (28 Jun 2007 08:27:16 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Thu, 28 Jun 2007 08:27:16 +0000 (UTC)
In-Reply-To: <[email protected]>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4,gzip(gfe),gzip(gfe)
Complaints-To: (e-mail address removed)
Injection-Info: q69g2000hsb.googlegroups.com; posting-host=80.65.75.8;
posting-account=S71-wA0AAADPO5Yb4mZw2xs1iHD3tEJH
Xref: uni-berlin.de comp.lang.c++:977090


Joseph Paterson je napisao:

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;

this->m_counter = 10;

See:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19
}

} ;

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

Regards,
Sumit.
 
J

Joseph Paterson

Exactly what I needed, thanks!
Apologies for omitting the ';' at the end of the class declarations!

Joseph Paterson.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top