C ++ is a simple handle class template and handle class template.

Source: Internet
Author: User

C ++ is a simple handle class template and handle class template.

#ifndef HANDLE_H#define HANDLE_H #include "Animal.h"template <typename T>class Handle{    public:        Handle(T *ptr);        Handle(const Handle &other);        Handle &operator = (const Handle &other);        ~Handle();        T *operator->();    private:        T *ptr_;};template <typename T>inline Handle<T>::Handle(T *ptr)    :ptr_(ptr->copy()){}template <typename T>inline Handle<T>::Handle(const Handle &other)    :ptr_(other.ptr_->copy()){}template <typename T>inline Handle<T> &Handle<T>::operator = (const Handle &other){    if(this != &other){        delete ptr_;        ptr_ = other.ptr_->copy();    }    return *this;}template <typename T>inline Handle<T>::~Handle(){    delete ptr_;}template <typename T>inline T *Handle<T>::operator -> (){    return ptr_;}#endif  /*HANDLE_H*/


A simple question about C ++ class templates

Let's see.
Template <class TYPE> // define a function template

TYPE max (TYPE x, TYPE y)
{
Return (x. value> y. value )? X. value: y. value;
}
The template function max accepts two parameters of the same type and returns the same value as the parameter type. However, when you call it like this:
Max (A, B );
Type is instantiated as the Type Mytype, that is, max is special
Mytype max (Mytype A, Mytype B );
Because you returned a Mytype: value, which is an integer, the compiler looked at it and found that the returned type should be Mytype! It is also found that the Mytype constructor can accept an integer value. Therefore, after compilation, a temporary Mytype object is constructed and returned using the int value you want to return. It is equivalent:
Return Mytype (x. value> y. value )? X. value: y. value );
So, max returns an object of the Mytype. If you do not overload the <operator, cout <max (A, B) <endl; returns an error! Add a. value to the end, because it is an int!

C ++ compiles a program that uses class templates to sort, search, and element arrays.

I'm so tired of writing it. Give it to me.
# Include <iostream. h>

# Include <iomanip. h>

Template <class T>

Class Array

{

T * set;

Int n;

Public:

Array (T * data, int I) {set = data; n = I ;}

~ Array (){}

Void sort (); // sort

Int seek (T key); // search for the specified Element

T sum (); // sum

Void disp (); // display all elements

};

Template <class T>

Void Array <T >:: sort ()

{

Int I, j;

T temp;

For (I = 1; I <n; I ++)

For (j = n-1; j> = I; j --)

If (set [J-1]> set [j])

{

Temp = set [J-1]; set [J-1] = set [j]; set [j] = temp;

}

}

Template <class T>

Int Array <T>: seek (T key)

{

Int I;

For (I = 0; I <n; I ++)

If (set = key)

Return I;

Return-1;

}

Template <class T>

T Array <T>: sum ()

{

T s = 0; int I;

For (I = 0; I <n; I ++)

S + = set;

Return s;

}

Template <class T>

Void Array <T >:: disp ()

{

Int I;

For (I = 0; I <n; I ++)

Cout <set <;

Cout <endl;

}

Void main ()

{

Int a [] = {6, 3, 8, 1, 9, 4, 7, 5, 2 };

Double B [] = {2.3, 6.1, 1.5, 8.4, 6.7, 3.8 };

Array <int> arr1 (a, 9 );

Array <double> arr2 (B, 6 );

Cout <arr1: <endl;

Cout <original sequence:; arr1.disp ();

Cout <8 position in arr1: <arr1.seek (8) <endl;

Arr1.sort ();

Cout <sorted:; arr1.disp ();

Cout <arr2: <endl;

Cout <original sequence:; arr2.disp ();

Cout <8.4 position in arr2: <arr2.seek (8.4) <endl;

Arr2.sort ();

Cout <sorted:; arr2.disp ();

}... Remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.