Scope visibility

Discussion in 'C++' started by Kaba, Mar 13, 2006.

  1. Kaba

    Kaba Guest

    Why doesn't the following work?

    template <typename Type>
    class A
    {
    public:
    Type data_;
    };

    template <typename Type>
    class B: public A<Type>
    {
    public:
    void set()
    {
    data_ = 4;
    }
    };

    int main()
    {
    B<int> b;
    b.set();
    return 0;
    }

    If you declare "using A<Type>::data_;" then it works. If you use normal
    classes, this works without "using". I don't get it. Tested with
    msvc2005 and comeau. Words from the standard?

    --
    Kalle Rutanen
    http://kaba.hilvi.org
     
    Kaba, Mar 13, 2006
    #1
    1. Advertising

  2. Kaba

    Steve Guest

    On 13/3/06 22:43, in article ,
    "Kaba" <> wrote:

    > Why doesn't the following work?
    >
    > template <typename Type>
    > class A
    > {
    > public:
    > Type data_;
    > };
    >
    > template <typename Type>
    > class B: public A<Type>
    > {
    > public:
    > void set()
    > {
    > data_ = 4;
    > }
    > };
    >
    > int main()
    > {
    > B<int> b;
    > b.set();
    > return 0;
    > }
    >
    > If you declare "using A<Type>::data_;" then it works.


    But do you understand why that works?

    > If you use normal
    > classes, this works without "using". I don't get it. Tested with
    > msvc2005 and comeau. Words from the standard?


    At the risk of doing your homework for you, have a read of

    <http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18>

    Apart from your example using a member variable and the example in the FAQ
    using a member function, they're both remarkably similar! ;-)

    --
    Regards,
    Steve

    "...which means he created the heaven and the earth... in the DARK! How good
    is that?"
     
    Steve, Mar 13, 2006
    #2
    1. Advertising

  3. Kaba

    Kaba Guest

    > > If you declare "using A<Type>::data_;" then it works.
    >
    > But do you understand why that works?
    >
    > > If you use normal
    > > classes, this works without "using". I don't get it. Tested with
    > > msvc2005 and comeau. Words from the standard?

    >
    > At the risk of doing your homework for you, have a read of
    >
    > <http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18>
    >
    > Apart from your example using a member variable and the example in the FAQ
    > using a member function, they're both remarkably similar! ;-)


    Thank you Steve, this answered my question. No this is not homework, I
    just found and switched off the "language extensions" option in visual
    studio and got errors I had not seen before:)

    --
    Kalle Rutanen
    http://kaba.hilvi.org
     
    Kaba, Mar 13, 2006
    #3
  4. Kaba

    Greg Comeau Guest

    In article <>,
    Kaba <> wrote:
    >Why doesn't the following work?
    >
    >template <typename Type>
    >class A
    >{
    >public:
    >Type data_;
    >};
    >
    >template <typename Type>
    >class B: public A<Type>
    >{
    >public:
    >void set()
    >{
    >data_ = 4;
    >}
    >};
    >
    >int main()
    >{
    >B<int> b;
    >b.set();
    >return 0;
    >}
    >
    >If you declare "using A<Type>::data_;" then it works. If you use normal
    >classes, this works without "using". I don't get it. Tested with
    >msvc2005 and comeau. Words from the standard?


    Have a look at
    http://www.comeaucomputing.com/techtalk/templates/#whymembernotfound
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Mar 14, 2006
    #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. Steve Wark
    Replies:
    1
    Views:
    600
    Brock Allen
    May 23, 2005
  2. Steve Wark

    Variable Scope / Visibility

    Steve Wark, May 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    470
    Steve Wark
    May 23, 2005
  3. TTroy

    Is "scope" different from "visibility" ?

    TTroy, Feb 10, 2005, in forum: C Programming
    Replies:
    8
    Views:
    312
    Dave Thompson
    Feb 14, 2005
  4. Steven T. Hatton
    Replies:
    1
    Views:
    411
    Steven T. Hatton
    Dec 13, 2006
  5. goodbadwolf

    Visibility vs Scope

    goodbadwolf, Mar 24, 2008, in forum: C++
    Replies:
    8
    Views:
    2,364
    James Kanze
    Mar 25, 2008
Loading...

Share This Page