Multi-level inheritance and accessing base protected member variables

Discussion in 'C++' started by Joseph Paterson, Jun 28, 2007.

  1. 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.
    Joseph Paterson, Jun 28, 2007
    #1
    1. Advertising

  2. Joseph Paterson

    Hari Guest

    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
    Hari, Jun 28, 2007
    #2
    1. Advertising

  3. Joseph Paterson

    Sumit Rajan Guest

    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, Jun 28, 2007
    #3
  4. Joseph Paterson

    Sumit Rajan Guest

    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 <>
    > 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: <>
    > References: <>
    > 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: <>
    > 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, Jun 28, 2007
    #4
  5. Exactly what I needed, thanks!
    Apologies for omitting the ';' at the end of the class declarations!

    Joseph Paterson.
    Joseph Paterson, Jun 28, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    2
    Views:
    397
    tom_usenet
    Aug 16, 2004
  2. Siemel Naran
    Replies:
    4
    Views:
    780
    Micah Cowan
    Jan 12, 2005
  3. pabbu
    Replies:
    8
    Views:
    700
    Marc Boyer
    Nov 7, 2005
  4. Joseph Paterson
    Replies:
    2
    Views:
    495
    Joseph Paterson
    Oct 25, 2006
  5. K. Frank
    Replies:
    8
    Views:
    621
    K. Frank
    Mar 22, 2012
Loading...

Share This Page