• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 297,  comments - 15,  trackbacks - 0
            Class templates can have friends. A class or class template, function, or function template can be a friend to a template class. Friends can also be specializations of a class template or function template, but not partial specializations.

            Example
            In the following example, a friend function is defined as a function template within the class template. This code produces a version of the friend function for every instantiation of the template. This construct is useful if your friend function depends on the same template parameters as the class does.

            // template_friend1.cpp
            // compile with: /EHsc

            #include <iostream>
            using namespace std;

            template <class T> class Array {
            T* array;
            int size;

            public:
            Array(int sz): size(sz) {
            array = new T[size];
            memset(array, 0, size * sizeof(T));
            }

            Array(const Array& a) {
            size = a.size;
            array = new T[size];
            memcpy_s(array, a.array, sizeof(T));
            }

            T& operator[](int i) {
            return *(array + i);
            }

            int Length() { return size; }

            void print() {
            for (int i = 0; i < size; i++)
            cout << *(array + i) << " ";

            cout << endl;
            }

            template<class T>
            friend Array<T>* combine(Array<T>& a1, Array<T>& a2);
            };

            template<class T>
            Array<T>* combine(Array<T>& a1, Array<T>& a2) {
            Array<T>* a = new Array<T>(a1.size + a2.size);
            for (int i = 0; i < a1.size; i++)
            (*a)[i] = *(a1.array + i);

            for (int i = 0; i < a2.size; i++)
            (*a)[i + a1.size] = *(a2.array + i);

            return a;
            }

            int main() {
            Array<char> alpha1(26);
            for (int i = 0 ; i < alpha1.Length() ; i++)
            alpha1[i] = 'A' + i;

            alpha1.print();

            Array<char> alpha2(26);
            for (int i = 0 ; i < alpha2.Length() ; i++)
            alpha2[i] = 'a' + i;

            alpha2.print();
            Array<char>*alpha3 = combine(alpha1, alpha2);
            alpha3->print();
            delete alpha3;
            }
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
            a b c d e f g h i j k l m n o p q r s t u v w x y z
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z

            The next example involves a friend that has a template specialization. A function template specialization is automatically a friend if the original function template is a friend.

            It is also possible to declare only the specialized version of the template as the friend, as the comment before the friend declaration in the following code indicates. If you do this, you must put the definition of the friend template specialization outside of the template class.


            // template_friend2.cpp
            // compile with: /EHsc
            #include <iostream>
            using namespace std;

            template <class T>
            class Array;

            template <class T>
            void f(Array<T>& a);

            template <class T> class Array
            {
            T* array;
            int size;

            public:
            Array(int sz): size(sz)
            {
            array = new T[size];
            memset(array, 0, size * sizeof(T));
            }
            Array(const Array& a)
            {
            size = a.size;
            array = new T[size];
            memcpy_s(array, a.array, sizeof(T));
            }
            T& operator[](int i)
            {
            return *(array + i);
            }
            int Length()
            {
            return size;
            }
            void print()
            {
            for (int i = 0; i < size; i++)
            {
            cout << *(array + i) << " ";
            }
            cout << endl;
            }
            // If you replace the friend declaration with the int-specific
            // version, only the int specialization will be a friend.
            // The code in the generic f will fail
            // with C2248: 'Array<T>::size' :
            // cannot access private member declared in class 'Array<T>'.
            //friend void f<int>(Array<int>& a);

            friend void f<>(Array<T>& a);
            };

            // f function template, friend of Array<T>
            template <class T>
            void f(Array<T>& a)
            {
            cout << a.size << " generic" << endl;
            }

            // Specialization of f for int arrays
            // will be a friend because the template f is a friend.
            template<> void f(Array<int>& a)
            {
            cout << a.size << " int" << endl;
            }

            int main()
            {
            Array<char> ac(10);
            f(ac);

            Array<int> a(10);
            f(a);
            }
            10 generic
            10 int
            The next example shows a friend class template declared within a class template. The class template is then used as the template argument for the friend class. Friend class templates must be defined outside of the class template in which they are declared. Any specializations or partial specializations of the friend template are also friends of the original class template.

            // template_friend3.cpp
            // compile with: /EHsc
            #include <iostream>
            using namespace std;

            template <class T>
            class X
            {
            private:
            T* data;
            void InitData(int seed) { data = new T(seed); }
            public:
            void print() { cout << *data << endl; }
            template <class U> friend class Factory;
            };

            template <class U>
            class Factory
            {
            public:
            U* GetNewObject(int seed)
            {
            U* pu = new U;
            pu->InitData(seed);
            return pu;
            }
            };

            int main()
            {
            Factory< X<int> > XintFactory;
            X<int>* x1 = XintFactory.GetNewObject(65);
            X<int>* x2 = XintFactory.GetNewObject(97);

            Factory< X<char> > XcharFactory;
            X<char>* x3 = XcharFactory.GetNewObject(65);
            X<char>* x4 = XcharFactory.GetNewObject(97);
            x1->print();
            x2->print();
            x3->print();
            x4->print();
            }
            65
            97
            A
            a
            from:
            http://msdn.microsoft.com/en-us/library/f1b2td24.aspx


            posted on 2010-04-17 23:33 chatler 閱讀(373) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Template
            <2010年4月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個(gè)博客還是不錯(cuò),雖然做的東西和我不大相關(guān),覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲欧美日韩中文久久| 久久久久亚洲精品男人的天堂| 久久99久久99精品免视看动漫| 少妇久久久久久被弄到高潮| 久久伊人五月丁香狠狠色| 亚洲精品无码久久千人斩| 99精品国产99久久久久久97| 青青青国产精品国产精品久久久久| 久久996热精品xxxx| 色狠狠久久AV五月综合| 国内精品久久久久久不卡影院| 一本大道久久东京热无码AV| 亚洲国产精品无码久久98| .精品久久久麻豆国产精品| 思思久久99热免费精品6| 7777久久亚洲中文字幕| 久久久久av无码免费网| 精品无码久久久久久国产| 少妇久久久久久被弄高潮| 久久久久女教师免费一区| 午夜不卡久久精品无码免费 | 中文字幕久久亚洲一区| 俺来也俺去啦久久综合网| 伊人久久大香线蕉av一区| 91精品免费久久久久久久久| 国产亚洲精品自在久久| 亚洲综合熟女久久久30p| 日日狠狠久久偷偷色综合免费 | 伊人久久大香线蕉AV一区二区| 国产成人精品久久二区二区| 久久久久成人精品无码中文字幕| 国内精品久久久久久久久电影网| 久久精品中文字幕无码绿巨人| 东方aⅴ免费观看久久av| 久久SE精品一区二区| 久久香综合精品久久伊人| 欧美精品乱码99久久蜜桃| 国产欧美久久久精品影院| 久久免费99精品国产自在现线 | 久久亚洲国产成人影院网站| 国内精品久久久久久久涩爱|