Doubt regarding inheritance involving templates

Discussion in 'C++' started by darkstorm, Mar 29, 2005.

  1. darkstorm

    darkstorm Guest

    I have a doubt regarding inheritance involving templates

    Consider this:
    /////////////////////////////////////
    template<typename T>
    class A
    {
    private:
    T m_a;

    public:
    virtual void Draw() = 0;
    }
    /////////////////////////////////////
    template<typename T>
    class B : public A<T>
    {
    public:
    B(T w, T h);

    private:
    ///data members....
    }

    template<typename T>
    B<T>::B(T w, T h)
    {
    m_a = w * h;
    }

    Error:

    error C2248: 'A<T>::m_a' : cannot access private member declared in class 'A<T>'
    with
    [
    T=fp_type
    ]
    and
    [
    T=fp_type
    ]

    What is the reason behind it? Here I am using public inheritance....
     
    darkstorm, Mar 29, 2005
    #1
    1. Advertising

  2. * darkstorm:
    > I have a doubt regarding inheritance involving templates
    >
    > Consider this:
    > /////////////////////////////////////
    > template<typename T>
    > class A
    > {
    > private:
    > T m_a;
    >
    > public:
    > virtual void Draw() = 0;
    > }
    > /////////////////////////////////////
    > template<typename T>
    > class B : public A<T>
    > {
    > public:
    > B(T w, T h);
    >
    > private:
    > ///data members....
    > }
    >
    > template<typename T>
    > B<T>::B(T w, T h)
    > {
    > m_a = w * h;
    > }
    >
    > Error:
    >
    > error C2248: 'A<T>::m_a' : cannot access private member declared in class 'A<T>'


    This error message has nothing to do with templates.

    Private is private (almost always: you can override but not access).

    The best way to fix this, assuming you really want private data in class A,
    is to add a constructor for class A and an use an initializer list in the B
    constructor. If, on the other hand, you want A's members to be accessible
    to B, declare them protected instead of private. But that's not as good.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 29, 2005
    #2
    1. Advertising

  3. darkstorm schrieb:
    > I have a doubt regarding inheritance involving templates
    >
    > Consider this:
    > /////////////////////////////////////
    > template<typename T>
    > class A
    > {
    > private:
    > T m_a;
    >
    > public:
    > virtual void Draw() = 0;
    > }

    ^ missing ;
    > /////////////////////////////////////
    > template<typename T>
    > class B : public A<T>
    > {
    > public:
    > B(T w, T h);
    >
    > private:
    > ///data members....
    > }

    ^ missing ;

    > template<typename T>
    > B<T>::B(T w, T h)
    > {
    > m_a = w * h;
    > }
    >
    > Error:
    >
    > error C2248: 'A<T>::m_a' : cannot access private member declared in class 'A<T>'
    >
    > What is the reason behind it? Here I am using public inheritance....


    No different than without templates. m_a is private to class A, no
    matter how B derives from it. To allow derived classes to access m_a,
    make it protected. In most cases protected data members are a sign of
    bad design though, A should provide a ctor that initialises m_a and an
    accessor:

    template< typename T >
    class A
    {
    private:
    T m_a;
    protected:
    A( const T& a ) : m_a( a ) {}
    const T& a() const { return m_a; }
    public:
    virtual void Draw() = 0;
    }

    template< typename T >
    class B : public A< T >
    {
    public:
    B( const T& w, const T& h )
    : A< T >( w * h ) {}

    private:
    ///data members....
    }

    HTH,
    Malte
     
    Malte Starostik, Mar 29, 2005
    #3
  4. Malte Starostik schrieb:
    > darkstorm schrieb:
    >
    >>template<typename T>
    >>class A
    >>{
    >>private:
    >> T m_a;
    >>
    >>public:
    >> virtual void Draw() = 0;
    >>}

    >
    > ^ missing ;
    >
    > template< typename T >
    > class A
    > {
    > private:
    > T m_a;
    > protected:
    > A( const T& a ) : m_a( a ) {}
    > const T& a() const { return m_a; }
    > public:
    > virtual void Draw() = 0;
    > }

    ^ well, and now the same error on my part *shakehead*

    Cheers,
    Malte
     
    Malte Starostik, Mar 29, 2005
    #4
    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. Russell Silva

    Style question involving inheritance

    Russell Silva, Jul 26, 2003, in forum: C++
    Replies:
    1
    Views:
    323
    Russell Silva
    Jul 26, 2003
  2. JKop
    Replies:
    3
    Views:
    480
  3. darkstorm
    Replies:
    3
    Views:
    498
    Victor Bazarov
    May 2, 2005
  4. recover
    Replies:
    2
    Views:
    814
    recover
    Jul 25, 2006
  5. Anarki
    Replies:
    5
    Views:
    469
    James Kanze
    Jul 7, 2007
Loading...

Share This Page