Connecting Tech Pros Worldwide Help | Site Map

Multi-level inheritance and accessing base protected member variables

Joseph Paterson
Guest
 
Posts: n/a
#1: Jun 28 '07
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-
Quote:
>m_counter, then it works fine.
Could anybody help me out with this one?

Thanks,

Joseph Paterson.

Hari
Guest
 
Posts: n/a
#2: Jun 28 '07

re: Multi-level inheritance and accessing base protected member variables



Joseph Paterson je napisao:
Quote:
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-
Quote:
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

Sumit Rajan
Guest
 
Posts: n/a
#3: Jun 28 '07

re: Multi-level inheritance and accessing base protected member variables


Hari wrote:
Quote:
Joseph Paterson je napisao:
Quote:
>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 <pasalic.zaharije@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: <1183019236.325811.203530@q69g2000hsb.googlegroups .com>
References: <1183018415.323759.182160@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: groups-abuse@google.com
NNTP-Posting-Date: Thu, 28 Jun 2007 08:27:16 +0000 (UTC)
In-Reply-To: <1183018415.323759.182160@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: groups-abuse@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:
Quote:
>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-
Quote:
>>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
Quote:
}
>
} ;
>
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 <sumit.rajan@gmail.com>
Sumit Rajan
Guest
 
Posts: n/a
#4: Jun 28 '07

re: Multi-level inheritance and accessing base protected member variables


Joseph Paterson wrote:
Quote:
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-
Quote:
>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 <sumit.rajan@gmail.com>
Joseph Paterson
Guest
 
Posts: n/a
#5: Jun 28 '07

re: Multi-level inheritance and accessing base protected member variables


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

Joseph Paterson.

Closed Thread