Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Redefintion of a function in template class

Reply
Thread Tools

Redefintion of a function in template class

 
 
syang8
Guest
Posts: n/a
 
      02-28-2011
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-28-2011
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
 
Reply With Quote
 
 
 
 
syang8
Guest
Posts: n/a
 
      02-28-2011
Make sense. Thanks.


On Feb 28, 12:27*pm, Victor Bazarov <(E-Mail Removed)> 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


 
Reply With Quote
 
ralph
Guest
Posts: n/a
 
      03-02-2011
On Mon, 28 Feb 2011 09:38:25 -0800 (PST), syang8 <(E-Mail Removed)>
wrote:

>Make sense. Thanks.
>
>
>On Feb 28, 12:27*pm, Victor Bazarov <(E-Mail Removed)> wrote:


>>


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


lol

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Class redefintion problem Marcus Kwok C++ 7 03-20-2007 01:11 AM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments