Redefintion of a function in template class

Discussion in 'C++' started by syang8, Feb 28, 2011.

  1. syang8

    syang8 Guest

    Hi all,

    I'd like to ask the reason the following code works. Original, I
    expected that there is going to be a "function redefinition" error.
    Could anyone tell me whether this is a compiler specific (I am using
    GNU g++ 4.3.4) issue or this actually conforms to the C++ standard? I
    appreciate.

    A function f is defined in a template class A. Class B inherited from
    A<int> redefines the function f in A<int>. The output shows that the
    definition f in A<int> is actually changed, while the other instance
    of the template, e.g. A<float>, remains untouched.

    //===================================
    #include <iostream>

    template<class T>
    class A
    {
    public:

    typedef A<T> BaseT;
    int f();
    };

    template<class T>
    int A<T>::f()
    {
    return 1;
    }

    class B : public A<int>
    {
    };

    template<>
    int
    B::BaseT::f()
    {
    return 3;
    }

    int main()
    {
    A<int> a1;
    A<float> a2;
    B b;
    std::cout << a1.f() << std::endl;
    std::cout << a2.f() << std::endl;
    std::cout << b.f() << std::endl;
    }

    // ======================================
    // The output of the program is
    // 3
    // 1
    // 3
    syang8, Feb 28, 2011
    #1
    1. Advertising

  2. On 2/28/2011 11:56 AM, syang8 wrote:
    > I'd like to ask the reason the following code works. Original, I
    > expected that there is going to be a "function redefinition" error.
    > Could anyone tell me whether this is a compiler specific (I am using
    > GNU g++ 4.3.4) issue or this actually conforms to the C++ standard? I
    > appreciate.
    >
    > A function f is defined in a template class A. Class B inherited from
    > A<int> redefines the function f in A<int>. The output shows that the
    > definition f in A<int> is actually changed, while the other instance
    > of the template, e.g. A<float>, remains untouched.


    What you have here is called a "specialization" of the template member.
    Read about it.

    >
    > //===================================
    > #include<iostream>
    >
    > template<class T>
    > class A
    > {
    > public:
    >
    > typedef A<T> BaseT;
    > int f();
    > };
    >
    > template<class T>
    > int A<T>::f()
    > {
    > return 1;
    > }
    >
    > class B : public A<int>
    > {
    > };
    >
    > template<>
    > int
    > B::BaseT::f()
    > {
    > return 3;
    > }
    >
    > int main()
    > {
    > A<int> a1;
    > A<float> a2;
    > B b;
    > std::cout<< a1.f()<< std::endl;
    > std::cout<< a2.f()<< std::endl;
    > std::cout<< b.f()<< std::endl;
    > }
    >
    > // ======================================
    > // The output of the program is
    > // 3
    > // 1
    > // 3


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 28, 2011
    #2
    1. Advertising

  3. syang8

    syang8 Guest

    Make sense. Thanks.


    On Feb 28, 12:27 pm, Victor Bazarov <> wrote:
    > On 2/28/2011 11:56 AM, syang8 wrote:
    >
    > > I'd like to ask the reason the following code works. Original, I
    > > expected that there is going to be a "function redefinition" error.
    > > Could anyone tell me whether this is a compiler specific (I am using
    > > GNU g++ 4.3.4) issue or this actually conforms to the C++ standard? I
    > > appreciate.

    >
    > > A function f is defined in a template class A. Class B inherited from
    > > A<int>  redefines the function f in A<int>. The output shows that the
    > > definition f in A<int>  is actually changed, while the other instance
    > > of the template, e.g. A<float>, remains untouched.

    >
    > What you have here is called a "specialization" of the template member.
    >   Read about it.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > //===================================
    > > #include<iostream>

    >
    > > template<class T>
    > > class A
    > > {
    > > public:

    >
    > >      typedef A<T>  BaseT;
    > >      int f();
    > > };

    >
    > > template<class T>
    > > int A<T>::f()
    > > {
    > >      return 1;
    > > }

    >
    > > class B : public A<int>
    > > {
    > > };

    >
    > > template<>
    > > int
    > > B::BaseT::f()
    > > {
    > >      return 3;
    > > }

    >
    > > int main()
    > > {
    > >      A<int>  a1;
    > >      A<float>  a2;
    > >      B b;
    > >      std::cout<<  a1.f()<<  std::endl;
    > >      std::cout<<  a2.f()<<  std::endl;
    > >      std::cout<<  b.f()<<  std::endl;
    > > }

    >
    > > // ======================================
    > > // The output of the program is
    > > // 3
    > > // 1
    > > // 3

    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask
    syang8, Feb 28, 2011
    #3
  4. syang8

    ralph Guest

    On Mon, 28 Feb 2011 09:38:25 -0800 (PST), syang8 <>
    wrote:

    >Make sense. Thanks.
    >
    >
    >On Feb 28, 12:27 pm, Victor Bazarov <> wrote:


    >>


    >> V
    >> --
    >> I do not respond to top-posted replies, please don't ask


    lol
    ralph, Mar 2, 2011
    #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. christopher diggins
    Replies:
    16
    Views:
    749
    Pete Becker
    May 4, 2005
  2. Marcus Kwok

    Class redefintion problem

    Marcus Kwok, Mar 19, 2007, in forum: C++
    Replies:
    7
    Views:
    323
    Jim Langston
    Mar 20, 2007
  3. Replies:
    2
    Views:
    542
  4. jrwats
    Replies:
    1
    Views:
    890
    Anthony Williams
    Mar 1, 2010
  5. A L
    Replies:
    1
    Views:
    510
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page