protected

Discussion in 'C++' started by Jeff Perry, Aug 3, 2004.

  1. Jeff Perry

    Jeff Perry Guest

    When I try to compile the code below with gcc 3.4.1, I get an error.
    In order to get the code to compile, I have to specify i's scope by
    prepending 'B::' to 'i' in function D::F(). Can someone tell me why
    this is true?

    The non-template version of this class hierarchy works without the
    scope specifier on 'i'.

    template<typename T>
    class B {
    public:
    virtual void F () { }
    T i;
    };

    template<typename T>
    class D: public B<T> {
    public:
    virtual void F () { i = 1; } // error: 'i' undeclared
    // but 'B::i = 1;' works
    };

    int main ()
    {
    D<unsigned> d;
    d.F ();
    return 0;
    }

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Jeff Perry, Aug 3, 2004
    #1
    1. Advertising

  2. "Jeff Perry" <> wrote...
    > When I try to compile the code below with gcc 3.4.1, I get an error.
    > In order to get the code to compile, I have to specify i's scope by
    > prepending 'B::' to 'i' in function D::F(). Can someone tell me why
    > this is true?
    >
    > The non-template version of this class hierarchy works without the
    > scope specifier on 'i'.
    >
    > template<typename T>
    > class B {
    > public:
    > virtual void F () { }
    > T i;
    > };
    >
    > template<typename T>
    > class D: public B<T> {
    > public:
    > virtual void F () { i = 1; } // error: 'i' undeclared
    > // but 'B::i = 1;' works


    'i' is what is known as "dependent name". With templates the scope
    of the base class is not searched for name resolution unless you
    specifically instruct the compiler to do so by either doing B:: or
    this->.

    > };
    >
    > int main ()
    > {
    > D<unsigned> d;
    > d.F ();
    > return 0;
    > }
    >


    V
    Victor Bazarov, Aug 3, 2004
    #2
    1. Advertising

  3. On 8/3/2004 1:28 AM, Jeff Perry wrote:
    > When I try to compile the code below with gcc 3.4.1, I get an error.
    > In order to get the code to compile, I have to specify i's scope by
    > prepending 'B::' to 'i' in function D::F(). Can someone tell me why
    > this is true?


    I think it is true. In dynamic polymorphism (virtual)
    compiler need in that template qualified name
    using :: operator.

    > template<typename T>
    > class B {
    > public:
    > virtual void F () { }
    > T i;
    > };


    Why you defined F() that way ?
    Shouldn't it be pure virtual function ?

    virtual void F () = 0;

    Regards

    --

    Mateusz £oskot
    mateusz at loskot dot net

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    =?ISO-8859-2?Q?Mateusz_=A3oskot?=, Aug 3, 2004
    #3
  4. Jeff Perry

    Sumit Rajan Guest

    "Jeff Perry" <> wrote in message
    news:...

    > template<typename T>
    > class B {
    > public:
    > virtual void F () { }
    > T i;
    > };
    >
    > template<typename T>
    > class D: public B<T> {
    > public:
    > virtual void F () { i = 1; } // error: 'i' undeclared
    > // but 'B::i = 1;' works
    > };
    >
    > int main ()
    > {
    > D<unsigned> d;
    > d.F ();
    > return 0;
    > }


    This issue is now covered by the C++ FAQ:
    http://www.parashift.com/c -faq-lite/containers-and-templates.html#faq-34.17

    Regards,
    Sumit.
    --
    Sumit Rajan <>



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Sumit Rajan, Aug 4, 2004
    #4
  5. Jeff Perry

    Sharad Kala Guest

    "Jeff Perry" <> wrote in message
    news:...
    > When I try to compile the code below with gcc 3.4.1, I get an error.
    > In order to get the code to compile, I have to specify i's scope by
    > prepending 'B::' to 'i' in function D::F(). Can someone tell me why
    > this is true?


    That's because gcc 3.4.1 implements the two phase name lookup. Changing i to
    B<T>::i or this->i makes it a dependent name and delays its lookup. Read
    this FAQ -
    http://www.parashift.com/c -faq-lite/containers-and-templates.html#faq-34.1
    7

    -Sharad



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Sharad Kala, Aug 4, 2004
    #5
  6. Jeff Perry wrote:
    > When I try to compile the code below with gcc 3.4.1, I get an error.
    > In order to get the code to compile, I have to specify i's scope by
    > prepending 'B::' to 'i' in function D::F(). Can someone tell me why
    > this is true?

    <snip>

    See question 1 at
    <http://www.decadentplace.org.uk/womble/cplusplus/template-faq.html>.

    (I've just uploaded that FAQ after working on it occasionally over the
    last few months. I'd appreciate comments on anything that should be
    added or corrected.)

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Ben Hutchings, Aug 4, 2004
    #6
    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. Andreas Klemt
    Replies:
    2
    Views:
    549
    Andreas Klemt
    Jul 5, 2003
  2. clintonG

    Re: What means Protected WithEvents ?

    clintonG, Jul 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    428
    clintonG
    Jul 5, 2003
  3. Jon
    Replies:
    4
    Views:
    5,630
    Jonathan Williams
    Aug 7, 2003
  4. Philippe Meunier
    Replies:
    1
    Views:
    294
    Mike Moore [MSFT]
    Oct 24, 2003
  5. Stephen Miller
    Replies:
    5
    Views:
    9,090
    kaverao
    Sep 17, 2009
Loading...

Share This Page