Thread: Questions about Templates

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Questions about Templates

    We'll start with a quote


    Many C++ programs use common data structures like stacks, queues and lists. A program may require a queue of customers and a queue of messages. One could easily implement a queue of customers, then take the existing code and implement a queue of messages. The program grows, and now there is a need for a queue of orders. So just take the queue of messages and convert that to a queue of orders (Copy, paste, find, replace????). Need to make some changes to the queue implementation? Not a very easy task, since the code has been duplicated in many places. Re-inventing source code is not an intelligent approach in an object oriented environment which encourages re-usability. It seems to make more sense to implement a queue that can contain any arbitrary type rather than duplicating code. How does one do that? The answer is to use type parameterization, more commonly referred to as templates.

    C++ templates allow one to implement a generic Queue<T> template that has a type parameter T. T can be replaced with actual types, for example, Queue<Customers>, and C++ will generate the class Queue<Customers>. Changing the implementation of the Queue becomes relatively simple. Once the changes are implemented in the template Queue<T>, they are immediately reflected in the classes Queue<Customers>, Queue<Messages>, and Queue<Orders>.

    Templates are very useful when implementing generic constructs like vectors, stacks, lists, queues which can be used with any arbitrary type. C++ templates provide a way to re-use source code as opposed to inheritance and composition which provide a way to re-use object code.


    Now this would suggest, that a generic template could complete replace all Base classes, why derive anything from a base class if you could just use a template, and make it simple - pass the template instead of every instance of each individual derivedren to your function.


    C++ provides two kinds of templates: class templates and function templates. Use function templates to write generic functions that can be used with arbitrary types. For example, one can write searching and sorting routines which can be used with any arbitrary type. The Standard Template Library generic algorithms have been implemented as function templates, and the containers have been implemented as class templates.


    Would this suggest that you can't combine the two? You either need a function template, or a class template, but wait, classes can have functions, right? Okay, problem solved..

    Implementing template member functions is somewhat different compared to the regular class member functions. The declarations and definitions of the class template member functions should all be in the same header file. The declarations and definitions need to be in the same header file. Consider the following.



    Code:
    //B.H
    template <class t>
    class b
    {
    public:
    	b() ;
    	~b() ;
    } ;
    Code:
     // B.CPP
    #include "B.H"
    template <class t>
    b<t>::b()
    {
    }
    template <class t>
    b<t>::~b()
    {
    }
    Code:
     //MAIN.CPP
    #include "B.H"
    void main()
    {
    	 b<int> bi ;
    	 b <float> bf ;
    }



    Quote:
    When compiling B.cpp, the compiler has both the declarations and the definitions available. At this point the compiler does not need to generate any definitions for template classes, since there are no instantiations. When the compiler compiles main.cpp, there are two instantiations: template class B<int> and B<float>. At this point the compiler has the declarations but no definitions!



    Would this suggest that I can pass functions to a template simply by going

    Code:
    Code:
    class c : public b<T>
    {
    	void dostuff<T>()
    	{
    		std::cout << "rawr" << std::endl;
    	}
    };
    Notice the goal is the have individual and different subclasses of a base class, but having the base class take form of the individual classes for the simplicity of being able to pass a single b to a function, instead of a c, or a d, or an e.

    SO ultimately I could create a container of b's, such as a vector, but have b be any one of its many derivedrens in form, neat idea huh?

    Like this:


    Code:
    Scene_Objects.push_back(b);

    Nifty huh? cuz now b would be a multitude of different things, but we can pass b because we first, make it take the form of a derivedren, and then, pass it to the function that pushes it into a vector....

    If it all worked this way, that would be super amazing.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Hmm, I dont know Shamino. Got some solid code to test that?

    I don't know if you can inherite templated classes, but in public b<T>, T would be your type (should be int or float or something at exists) and what would dostuff<T> do? Did you meant to template c too with typename T? (you're using class t in your b)

    I see your idea, but templates are compile-time so you're still resulting with a cast to some base class. Which wouldn't work unless you were using the same type for the base class in the subclass as you were in the vector of the base class.

    ie. I'm going to assume you meant to template c too, because theres two outcomes:

    Templated c and used public b<T> in order to change b to whats passed (int in this case):
    vector< b<int> > Something;
    c<int> Another;
    Something.push_back(Another).
    I think would result in a cast of c to b, which means the loss of dostuff();.

    Changed it to public a<int>:
    vector< b<int> > Something;
    c Another;
    Something.push_back(Another).
    Would result in the same as the last.

    If you werent matching int to int and float to float and such then it would probably give you a casting error.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    No, I don't, cuz I don't know much about templates in the first place, I'm just asking if that psuedocode would work....

    you can pretty much ignore my syntax, cuz i suxxor at template syntax...

    I would like to inherit the templated class...

    We don't want to template C....

    C never changes, C is a C, thats that... We want B to turn into a C on command........

    Blehhhh......

    I know what I want my ultimate goal to be, can't anyone just gimme something based on that?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Now this would suggest, that a generic template could complete replace all Base classes,
    How so?

    why derive anything from a base class if you could just use a template
    Using a template and using inheritance are two different things. For example, instances of a templated class will all have the same functions, but derived classes can all have different functions.

    derivedren
    What does that mean? I have a 1,000 page beginning C++ book and that term is not in there. Furthermore, google gives 0 hits, which is quite a feat.


    I don't know much about templates in the first place
    I can't really figure out what you are asking, but maybe you should try studying templates and how to use them, as well as studying inheritance and polymorphism and how to use them, and then you will be able to use templates and inheritance to suit your needs.
    Last edited by 7stud; 12-17-2005 at 10:39 PM.

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Theres actually only one webpage that uses that term "derivedren", and thats a tutorial on this site. http://www.cprogramming.com/tutorial.../lesson19.html

    Seems its shorthand for saying derived children, aka subclasses, or derived classes, or simply children (which is acceptable).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM