zoukankan      html  css  js  c++  java
  • c++官方文档-模版类

    #include <iostream>
    using namespace std;
    
    template<class T>
    class MyPair
    {
        private:
            T t[2];
            T a, b;
        public:
            MyPair(T t1, T t2)
                    : a(t1), b(t2)
            {
                t[0] = t1;
                t[1] = t2;
            }
            const T* getT()
            {
                return t;
            }
            T getMax();
    };
    /**
     * 第一个T指定模版参数
     * 第二个T指定函数返回的类型
     * 第三个T指定函数的模版参数是类的模版参数
     *
     */
    template<class T> T MyPair<T>::getMax()
    {
        //不知道retval的具体类型,所以返回一个临时变量
        T retval;
        retval = a > b ? a : b;
        return retval;
    }
    template<class R> R f(const R& t)
    {
        R r = t;
        //错误
    //    t= r;
        return r;
    }
    
    //为模版定义不同的实现
    template<class T>
    class mycontainer
    {
            T element;
        public:
            mycontainer(T args)
            {
                this->element = args;
            }
            T increase()
            {
                return ++element;
            }
    };
    
    //class template specialization
    //模版实例化
    //This is because all types
    //are known and no template arguments are required for this specialization
    //这个是因为所有类型都已经知道而且实例化并不需要模版参数
    template<>
    class mycontainer<char>
    {
            char element;
        public:
            mycontainer(char arg)
            {
                element = arg;
            }
            char uppercase()
            {
                if((element >= 'a') && (element <= 'z'))
                    element += 'A' - 'a';
                return element;
            }
    };
    //template <class T> class mycontainer { ... };//The first line is the generic template
    //template <> class mycontainer <char> { ... };//and the second one is the specialization
    
    int main()
    {
        //注释这句话会导致上面那个函数没有编译,能编译通过
        f<int>(2);
        MyPair<int> myint(1, 2);
        //非常量指针指向常量空间
        const int* t = myint.getT();
    //    t[0]=2000;//invalid
        cout << t[0] << endl;
        cout << myint.getT()[0] << endl;
        int tt[] = { 100, 200 };
        t = tt;
        cout << t[0] << endl;
        cout << myint.getT()[0] << endl;
        MyPair<double> mydouble(3.0, 4.0);
        cout << mydouble.getMax() << endl;
    
        mycontainer<int> myintcontainer(7);
        mycontainer<char> mychar('j');
        cout << myintcontainer.increase() << endl;
        cout << mychar.uppercase() << endl;
        return 0;
    }

    模版函数和模版类

    #include <iostream>
    using namespace std;
    
    class Example4
    {
            string* ptr;
        public:
            // constructors:
            Example4()
                    : ptr(new string)
            {
            }
            Example4(const string& str)
                    : ptr(new string(str))
            {
            }
            // destructor:
            ~Example4()
            {
                cout << "调用析构函数" << endl;
                delete ptr;
            }
            // access content:
            const string& content() const
            {
                return *ptr;
            }
    };
    
    template<class T>
    class myClass
    {
            T t;
        public:
            myClass(T t)
                    : t(t)
            {
    
            }
            template<class R> T f(R const& r, T const& t);
    };
    template<class T> template<class R> T myClass<T>::f(R const& r, T const& t)
    {
        cout << "r=" << r << endl;
        return t;
    }
    
    int main()
    {
        //The destructor for an object is called at the end of its lifetime;
        //in the case of foo and bar this happens at the end of function main.
        //析构函数在对象生命周期结束后被调用,在这个列子中,foo和bar在main函数的结束时结束
        //可以理解成从栈中弹出么
        Example4 foo;
        Example4 bar("Example");
        Example4* fp = new Example4("Example pointer");
        cout << "bar's content: " << bar.content() << '
    ';
        cout << "fp's content: " << fp->content() << '
    ';
        //指针要手动调用,new 出来的对象,内存块在堆上,不在当前函数栈里
        //可以把下面这句话注释了看下
        delete fp;
    
        myClass<int> myint(10);
        int i = myint.f<double>(200.01, 10);
        cout << i << endl;
        return 0;
    }

    下面这个模式就有点奇怪了

    #include <iostream>
    using namespace std;
    
    class Example4
    {
            string* ptr;
        public:
            // constructors:
            Example4()
                    : ptr(new string)
            {
            }
            Example4(const string& str)
                    : ptr(new string(str))
            {
            }
            // destructor:
            ~Example4()
            {
                cout << "调用析构函数" << endl;
                delete ptr;
            }
            // access content:
            const string& content() const
            {
                return *ptr;
            }
    };
    
    template<class T>
    class myClass
    {
            T t;
        public:
            myClass(T t)
                    : t(t)
            {
    
            }
            template<class R> R f(R const& r, R const& t);
    };
     template<class T> template<class R> R myClass<T>::f(R const& r, R const& t)
    {
        cout << "r=" << r << endl;
        return t;
    }
    
    int main()
    {
        //The destructor for an object is called at the end of its lifetime;
        //in the case of foo and bar this happens at the end of function main.
        //析构函数在对象生命周期结束后被调用,在这个列子中,foo和bar在main函数的结束时结束
        //可以理解成从栈中弹出么
        Example4 foo;
        Example4 bar("Example");
        Example4* fp = new Example4("Example pointer");
        cout << "bar's content: " << bar.content() << '
    ';
        cout << "fp's content: " << fp->content() << '
    ';
        //指针要手动调用,new 出来的对象,内存块在堆上,不在当前函数栈里
        //可以把下面这句话注释了看下
        delete fp;
    
        myClass<int> myint(10);
        cout << myint.f<double>(100.0, 100.0)<<endl;
        return 0;
    }
  • 相关阅读:
    表单元素的required,autocomplete,list用法
    通用triggerEvent方法
    通用addEventListener方法
    关于动态加载js
    Node路由简单的处理
    As3截图转换为ByteArray传送给后台node的一种方法
    JS中的“==”符号及布尔值转换规则
    解释JS变量作用域的范例
    .NET异步委托
    正则表达式入门
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/8449128.html
Copyright © 2011-2022 走看看