Templates
This presentation is the property of its rightful owner.
Sponsored Links
1 / 8

Templates PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on
  • Presentation posted in: General

Templates. a way of writing generic code. Questions about Templates. what is a template? why are templates needed? how is a function template declared? what does this function prototype mean: template<class T> void myfunc(int*,const T&, T*);

Download Presentation

Templates

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Templates

Templates

a way of writing generic code


Questions about templates

Questions about Templates

  • what is a template?

  • why are templates needed?

  • how is a function template declared?

  • what does this function prototype mean:

    template<class T>

    void myfunc(int*,const T&, T*);

  • how does the compiler figure out what class/type to substitute for the template parameter?

  • how is templated function invoked?

  • is template an executable statement?


Why templates

Why Templates

  • certain code operates similarly regardless of type

    • example: swapping two values

  • templates parameterize type and allow writing generic code


Standalone function templates

Standalone Function Templates

  • function head/prototype are preceded by

    template <class typeParameter>

  • style convention – always put on separate line

  • example

    template <class T>

    void print_stuff(int a, T b, T c);

  • type parameter may be passed by reference/returned/declared constant

    template <class T>

    void show_stuff(int, T&, const T&);

  • in definition

    template <class T>

    void show_stuff(int a, T& b, const T& c){

    cout << a << b << c;

    }


Template invocation

Template Invocation

  • invoked as ordinary function

    double one=3.5, two=5.6;

    string str1="one", str2="two";

    show_stuff(1, one, two);

    show_stuff(3, str1, str2);

  • templates are not executable code

    • compiler generates executable code when it sees function invocation. This is called template instantiation

      • the type parameter is determined by the types of the arguments

    • can be placed in headers


Class templates

Class Templates

  • class definition is preceded by

    template <class typeParameter>

  • example

    template <class T>

    myclass{

    private:

    int a;

    T b;

    T *c;

    };

  • member functions can be declared inline or outside

    template <class T>

    void myclass<T> show_stuff(int, T&, const T&);


Member function definition

Member Function Definition

  • member functions can be defined

    • inline

      template <class T>

      yourclass{

      public: int geta() const{return a;};

      T getb() const;

      void setc(T *const);

      private:

      int a; T b; T *c;

      };

    • or outside – use scope resolution operator and template keyword

      template<class T>

      T yourclass<T>::getb() const {

      return b;

      }


Object declaration and usage

Object Declaration and Usage

  • to use template - declare an object of the class, have to explicitly specify type parameter

    yourclass<double> d1, *pd1;

  • methods are invoked as on ordinary objects

    pd1=&d1;

    cout << d1.geta();

    cout << pd1->getb();

  • class templates are not executable, compiler instantiates class templates when objects are declared, methods are instantiated when invoked

    • put class templates in header files


  • Login