青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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 閱讀(378) 評論(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
  • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

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

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美日韩精品免费观看视一区二区 | 久久黄金**| 一区二区国产日产| 9久re热视频在线精品| 日韩天堂在线观看| 亚洲少妇中出一区| 亚洲女女女同性video| 夜夜嗨一区二区| 国产精品99久久久久久www| 中文在线不卡| 欧美亚洲综合在线| 另类春色校园亚洲| 亚洲国产精品va| 亚洲人成人99网站| 在线亚洲精品福利网址导航| 亚洲女性裸体视频| 久久亚洲私人国产精品va媚药| 欧美激情 亚洲a∨综合| 亚洲激情一区| 亚洲在线网站| 久久综合久久美利坚合众国| 欧美日韩国产精品一区二区亚洲 | 亚洲国产精品女人久久久| 日韩亚洲欧美成人| 国产日韩欧美在线播放不卡| 欧美视频在线观看一区| 鲁大师影院一区二区三区| 久久久久九九九九| 免费不卡视频| 欧美日韩国产三级| 国产亚洲精品高潮| 在线精品视频在线观看高清| 亚洲区中文字幕| 欧美一区二区三区日韩| 欧美激情一区二区三区在线| 一区二区三区不卡视频在线观看| 欧美在线视频网站| 欧美视频精品在线| 日韩午夜剧场| 欧美国产精品劲爆| 小黄鸭精品密入口导航| 欧美日韩国产成人在线| 在线观看国产日韩| 久久精品72免费观看| 一区二区三区高清不卡| 另类激情亚洲| 黄色成人在线网址| 欧美一二三视频| 一区二区三区导航| 欧美三级精品| 一本色道久久| 亚洲人成在线影院| 米奇777在线欧美播放| 好吊色欧美一区二区三区视频| 一区二区三区欧美激情| 最新精品在线| 欧美美女bbbb| 99国产精品久久久| 亚洲日韩欧美视频一区| 女生裸体视频一区二区三区| 在线观看日韩av电影| 久久裸体艺术| 久久九九精品| 亚洲第一视频| 亚洲福利视频在线| 欧美精品1区2区| 在线亚洲欧美| 一区二区三区高清| 国产欧美精品在线播放| 久久激情视频久久| 欧美在线视频网站| 亚洲国产精品传媒在线观看| 蜜臀av在线播放一区二区三区| 亚洲第一视频网站| 国产精品v亚洲精品v日韩精品 | 亚洲一品av免费观看| 国产精品日韩欧美大师| 欧美制服丝袜第一页| 欧美在线播放一区| 亚洲国产精品一区制服丝袜 | 亚洲精品久久久久久久久久久久 | 国产精品99久久久久久久久久久久| 亚洲人久久久| 国产精品一香蕉国产线看观看| 欧美一级网站| 久久人体大胆视频| 在线亚洲精品福利网址导航| 亚洲中字在线| 亚洲风情在线资源站| 亚洲精品国产无天堂网2021| 国产精品电影在线观看| 久久久国产91| 欧美精品一区二区三区一线天视频 | 亚洲精选91| 国产一区二区三区久久久久久久久| 女仆av观看一区| 欧美午夜剧场| 欧美成人四级电影| 国产精品一级在线| 亚洲国产va精品久久久不卡综合| 国产精品久久二区| 亚洲国产一区二区三区a毛片| 国产精品美女黄网| 亚洲激情自拍| 狠狠干综合网| 99精品欧美一区二区蜜桃免费| 国内精品视频在线观看| 99精品欧美一区二区三区| 伊人婷婷久久| 欧美一区二区三区视频在线观看| av成人激情| 你懂的视频一区二区| 久久精品视频在线看| 国产精品a久久久久久| 欧美激情视频免费观看| 国内精品久久久久影院优| 亚洲一二三区精品| 一区二区三区四区国产精品| 老色批av在线精品| 久久久亚洲国产天美传媒修理工| 国产精品ⅴa在线观看h| 亚洲高清色综合| 在线观看欧美一区| 久久精品欧美| 久久夜色精品国产亚洲aⅴ| 国产精品影院在线观看| 一区二区黄色| 亚洲专区免费| 午夜欧美视频| 亚洲欧美日韩国产综合| 亚洲男人的天堂在线| 亚洲精品美女在线观看| 国产综合色在线视频区| 亚洲自拍啪啪| 亚洲欧美激情视频在线观看一区二区三区 | 麻豆91精品| 国内成+人亚洲| 欧美一级理论性理论a| 欧美在线影院| 国产日韩综合| 欧美在线综合视频| 久久影院亚洲| 在线日本成人| 欧美激情一区二区三区成人| 亚洲国产黄色片| 99这里只有精品| 国产精品草草| 午夜精品久久久久久久久久久久久| 欧美一区国产一区| 黄色精品一区二区| 欧美成人激情视频| 日韩亚洲欧美高清| 欧美亚洲视频一区二区| 国产亚洲人成a一在线v站| 久久精品综合一区| 亚洲国产精品一区二区www| 一本色道久久加勒比88综合| 欧美日精品一区视频| 亚洲一区二区三区四区在线观看| 欧美亚洲免费电影| 在线观看国产一区二区| 欧美日本国产一区| 亚洲欧美日韩国产另类专区| 久久精品系列| 亚洲靠逼com| 国产日韩欧美在线观看| 欧美不卡视频| 亚洲欧美一区二区三区在线| 麻豆精品视频| 亚洲一级一区| 伊甸园精品99久久久久久| 欧美精品一区二区三区在线看午夜| 亚洲视频网在线直播| 久久日韩粉嫩一区二区三区| 夜夜嗨av一区二区三区四季av| 国产精品日韩欧美一区二区三区| 久久三级福利| 亚洲综合色自拍一区| 亚洲国产高清一区| 久久九九国产| 亚洲欧美日韩精品综合在线观看| 尤物yw午夜国产精品视频明星| 欧美三级免费| 女同一区二区| 久久精品国产亚洲一区二区三区 | 久久激情网站| 宅男噜噜噜66国产日韩在线观看| 激情综合色丁香一区二区| 欧美日韩国产三级| 毛片基地黄久久久久久天堂| 亚洲制服欧美中文字幕中文字幕| 99国内精品| 国语对白精品一区二区| 国产精品99一区二区| 欧美激情无毛| 蜜臀91精品一区二区三区| 欧美一区二区三区在线播放| 在线视频精品| 一区二区三区精品在线| 亚洲国产激情|