class templates

This is a discussion on class templates within the C++ Programming forums, part of the General Programming Boards category; Code: template <class T> class Sample { public: Sample() ; ~Sample() ; private: T a; }; Code: template <class T> ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    class templates

    Code:
    template <class T>
    class Sample
    {
    public:
    	Sample() ;
    	~Sample() ;
    private:
    	T a;
    };
    Code:
    template <class T>
    Sample<T>::Sample() {a = 0;}
    
    template <class T>
    Sample<T>::~Sample() {}
    When defining the constructor ? Why do we write The class name as Sample<T> why not only Sample::Sample()

    ???

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Because it's a template, so the <T> is part of the type, and because that's how the standard was written.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    can we do this ?

    Code:
    template <class T>
    class Sample
    {
    public:
    	Sample() ;
    	~Sample() ;
    private:
    	T a;
    };
    Code:
    template <class C>
    Sample<C>::Sample() {a = 0;}
    
    template <class A>
    Sample<A>::~Sample() {}

    what I want to say is that is the type parameter independent of the type parameter of the class ?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can try compiling it and find out, but it works for me on the Comeau compiler.

    I don't see much point in separating the function definitions from the class anyways, since all of it must be in the header file anyways.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Quote Originally Posted by manzoor View Post
    can we do this ?

    Code:
    template <class T>
    class Sample
    {
    public:
    	Sample() ;
    	~Sample() ;
    private:
    	T a;
    };
    Code:
    template <class C>
    Sample<C>::Sample() {a = 0;}
    
    template <class A>
    Sample<A>::~Sample() {}
    what I want to say is that is the type parameter independent of the type parameter of the class ?
    The compiler doesn't care if the names are different, so long as the parameters in the template are the same.

    Quote Originally Posted by cpjust View Post
    I don't see much point in separating the function definitions from the class anyways, since all of it must be in the header file anyways.
    Because it's easier to read if the implementation is split from the definition?
    I typically split the implementation from the definition, even in template classes.
    You can just put the definition on top and the implementation just below. This is what I typically do for template classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    The function implementation is also a definition. You're thinking of implementation and declaration.
    Last edited by robwhit; 10-14-2008 at 10:17 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Quote Originally Posted by robwhit View Post
    The function implementation is also a definition.
    But it's not.
    A function prototype is a declaration and the definition is also an implementation, but classes are a different story.
    The class layout is the definition, and the code is the implementation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Quote Originally Posted by Elysia View Post
    But it's not.
    A function prototype is a declaration and the definition is also an implementation, but classes are a different story.
    The class layout is the definition, and the code is the implementation.
    "Implementation" is best used to describe algorithms, not classes. The class layout is a class definition, which contains declarations for member functions. These member functions are then defined in source files. This is how the standard used these words.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Maybe that was messed up...
    But the terms I have heard and use are:
    Code:
    class Foo {}; // Class definition
    void Foo::foo() {} // Implementation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Is it not a little early in the week for trivial arguments over semantics?

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Because it's easier to read if the implementation is split from the definition?
    I typically split the implementation from the definition, even in template classes.
    You can just put the definition on top and the implementation just below. This is what I typically do for template classes.
    Most modern IDE's let you collapse blocks of code between braces such as functions, so that's usually good enough for me.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Visual Studio's such feature is frustrating since it keeps expanding them on several occasions that you do not want it to.
    But it does save having to type out a lot of extra crap all the time, so it's convenient... Hmmm...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    May I ask why we write the class name as

    Code:
    Sample<T>::Sample


    why a parameter is necessary there (in the text bold), can i knwo its purpose

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    Because you are able to overload functions for specific types. For instance if you want a typespecific implementation for T=int you can have that.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    why a parameter is necessary there (in the text bold), can i knwo its purpose
    Probably to show that this is a member function of a class template, not (just) a member function template of a class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Class templates and iterators
    By creativeinspira in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2007, 03:35 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21