Multi-level inheritance and accessing base protected member variables



 DEVELOP > c-Plus-Plus > Multi-level inheritance and accessing base protected member variables

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Joseph Paterson"
Date: 28 Jun 2007 03:13:35 AM
Object: Multi-level inheritance and accessing base protected member variables
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.
.

User: "Hari"

Title: Re: Multi-level inheritance and accessing base protected member variables 28 Jun 2007 03:27:16 AM
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
.
User: "Sumit Rajan"

Title: Re: Multi-level inheritance and accessing base protected member variables 28 Jun 2007 04:09:35 AM
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.googlegroups.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:


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:

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-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.
--
Sumit Rajan <sumit.rajan@gmail.com>
.
User: "Joseph Paterson"

Title: Re: Multi-level inheritance and accessing base protected member variables 28 Jun 2007 08:58:41 AM
Exactly what I needed, thanks!
Apologies for omitting the ';' at the end of the class declarations!
Joseph Paterson.
.



User: "Sumit Rajan"

Title: Re: Multi-level inheritance and accessing base protected member variables 28 Jun 2007 04:05:39 AM
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 <sumit.rajan@gmail.com>
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER