C++的一個簡單的控制代碼類模板,控制代碼類模板

來源:互聯網
上載者:User

C++的一個簡單的控制代碼類模板,控制代碼類模板

#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*/


C++類模板的一個簡單問題

咱來看
template <class TYPE> //定義函數模板

TYPE max(TYPE x,TYPE y)
{
return (x.value>y.value)?x.value:y.value;
}
模板函數max接受兩個相同類型的參數,返回與參數類型相同的值。但是,當你如此調用時:
max(A, B);
Type被執行個體化為類型Mytype,即此時max被特例化為
Mytype max(Mytype A, Mytype B);
由於你返回的是一個Mytype::value,它是整型的,編譯器看了看,發現傳回型別應該是Mytype才對啊!又發現,Mytype的建構函式可以接受一個整型值,於是乎,編譯就用你要返回的int值構造了一個臨時的Mytype對象返回了。就相當於:
return Mytype((x.value>y.value)?x.value:y.value);
所以說,max返回的是一個Mytype類型的對象,你又沒有重載<<操作符,那麼cout<<max(A, B)<<endl;就報錯了!後面加個.value也就行了,因為那是個int!
 
C ++編寫一個使用類模板對數組進行排序、尋找與元素與的程式

寫的我好累,分給我吧
#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(); // 排序

int seek(T key); // 尋找指定的元素

T sum(); // 求和

void disp(); // 顯示所有的元素

};

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<< 原序列:; arr1.disp();

cout<< 8在arr1中的位置:<<arr1.seek(8)<<endl;

arr1.sort();

cout<< 排序後:; arr1.disp();

cout<<arr2:<<endl;

cout<< 原序列:; arr2.disp();

cout<< 8.4在arr2中的位置:<<arr2.seek(8.4)<<endl;

arr2.sort();

cout<< 排序後:; arr2.disp();

}...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.