By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,034 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,034 IT Pros & Developers. It's quick & easy.

Multi-level inheritance and accessing base protected member variables

P: n/a
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.

Jun 28 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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, inta;
a.foo();
}

P.S.
You can always test code on multiple compilers at http://dinkumware.com/exam/
..

Best,
Pasalic Zaharije

Jun 28 '07 #2

P: n/a
Joseph Paterson wrote:
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?
How about using this->m_counter instead of "just using" m_counter? Does
that fix your problem?


--
Sumit Rajan <su*********@gmail.com>
Jun 28 '07 #3

P: n/a
Hari wrote:
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
{
Path: uni-berlin.de!fu-berlin.de!postnews.google.com!q69g2000hsb.googlegr oups.com!not-for-mail
From: Hari <pa**************@gmail.com>
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: <11**********************@q69g2000hsb.googlegroups .com>
References: <11**********************@m36g2000hse.googlegroups .com>
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: gr**********@google.com
NNTP-Posting-Date: Thu, 28 Jun 2007 08:27:16 +0000 (UTC)
In-Reply-To: <11**********************@m36g2000hse.googlegroups .com>
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: gr**********@google.com
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:
>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;
this->m_counter = 10;

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

} ;

int main()
{
HashMap<int, inta;
a.foo();
}

P.S.
You can always test code on multiple compilers at http://dinkumware.com/exam/
..

Best,
Pasalic Zaharije
Regards,
Sumit.

--
Sumit Rajan <su*********@gmail.com>
Jun 28 '07 #4

P: n/a
Exactly what I needed, thanks!
Apologies for omitting the ';' at the end of the class declarations!

Joseph Paterson.

Jun 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.