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

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 閱讀(393) 評論(0)  編輯 收藏 引用 所屬分類: Template
<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用鏈接

留言簿(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>
            亚洲高清资源| 亚洲理论在线| 欧美一区二区三区在线观看| 国产精品免费区二区三区观看| 亚洲午夜精品国产| 亚洲黄色免费电影| 欧美激情一区二区三级高清视频| 噜噜噜噜噜久久久久久91| 亚洲精品国产无天堂网2021| 亚洲精品久久久蜜桃| 欧美四级电影网站| 欧美伊人久久久久久午夜久久久久 | 久久国产精品免费一区| 狠色狠色综合久久| 亚洲欧洲日本专区| 国产精品系列在线播放| 久久影视三级福利片| 免费看av成人| 午夜在线不卡| 久久一区精品| 亚洲在线观看免费视频| 美女网站在线免费欧美精品| 美女国产精品| 午夜精品美女久久久久av福利| 欧美激情网站在线观看| 欧美日韩在线免费视频| 久久夜色精品国产欧美乱极品| 卡通动漫国产精品| 亚洲欧美色婷婷| 久久午夜精品| 午夜日韩电影| 欧美mv日韩mv国产网站app| 亚洲欧美在线免费| 久久亚洲高清| 欧美一区二区视频在线观看2020 | 欧美激情在线有限公司| 国产精品v日韩精品| 蜜桃久久av一区| 国产精品国产一区二区| 欧美成人国产| 国产日本欧洲亚洲| 9人人澡人人爽人人精品| 亚洲第一久久影院| 欧美一区二区久久久| 亚洲视频免费| 欧美激情一区二区三区在线视频观看| 久久国产精品免费一区| 欧美日韩系列| 亚洲欧洲一区二区天堂久久| 黄色成人免费网站| 午夜精品久久久久久久99樱桃| 亚洲最新中文字幕| 欧美a级大片| 免费观看成人| 精品动漫3d一区二区三区| 亚洲男女自偷自拍| 亚洲综合精品| 欧美调教视频| 日韩一区二区福利| 一区二区免费在线播放| 欧美激情精品久久久久久黑人| 免费中文日韩| 1024国产精品| 久久天天躁狠狠躁夜夜av| 久久久欧美精品| 国产日韩欧美在线播放| 午夜视频在线观看一区二区| 午夜精品久久久久久久蜜桃app | 国产午夜亚洲精品羞羞网站| 亚洲色图综合久久| 亚洲欧美999| 国产精品一卡| 欧美一级网站| 久久综合中文字幕| 欧美精品色综合| 亚洲人成人一区二区三区| 亚洲巨乳在线| 欧美色区777第一页| 99精品欧美| 欧美在线1区| 国产综合色产在线精品| 久久久青草婷婷精品综合日韩| 狂野欧美激情性xxxx欧美| 激情婷婷亚洲| 男人的天堂成人在线| 91久久精品日日躁夜夜躁国产| 亚洲精品影院| 国产精品乱子久久久久| 午夜影院日韩| 欧美国产精品| 亚洲小说欧美另类社区| 国产欧美精品一区 | 欧美激情1区| 在线一区视频| 国产自产女人91一区在线观看| 久久久www成人免费毛片麻豆| 欧美激情亚洲视频| 亚洲一区二区三区影院| 国产一区二区三区久久 | 亚洲女优在线| 欧美成人午夜激情视频| 亚洲先锋成人| 激情成人中文字幕| 欧美日韩精品不卡| 欧美一区二区三区在线观看| 亚洲国产美女| 欧美在线播放| 9色国产精品| 国产一区二区三区丝袜| 欧美精品一区二区精品网| 亚洲欧美日韩爽爽影院| 亚洲狠狠婷婷| 久久狠狠婷婷| 中文国产成人精品| 亚洲国产成人午夜在线一区| 国产精品家庭影院| 欧美凹凸一区二区三区视频| 亚洲在线第一页| 亚洲区一区二| 欧美成人激情视频免费观看| 亚洲欧美综合网| 日韩亚洲欧美高清| 精品91视频| 国产欧亚日韩视频| 欧美日韩在线一二三| 麻豆精品视频在线| 欧美在线二区| 久久免费黄色| 欧美一区精品| 亚洲一区网站| 中日韩美女免费视频网址在线观看 | 欧美日韩精品在线播放| 久久综合九色综合欧美狠狠| 午夜在线播放视频欧美| 在线一区视频| 艳妇臀荡乳欲伦亚洲一区| 亚洲国产经典视频| 女人天堂亚洲aⅴ在线观看| 久久精品人人| 欧美在线免费一级片| 午夜精品理论片| 亚洲欧美日韩网| 亚洲综合丁香| 亚洲综合色丁香婷婷六月图片| 亚洲精品你懂的| 亚洲精品国产精品国产自| 亚洲大片免费看| 亚洲国产精品专区久久| 亚洲国产精品久久91精品| 国内精品一区二区三区| 曰韩精品一区二区| 亚洲国产成人精品久久| 亚洲激情影院| 日韩一区二区精品视频| 日韩亚洲在线观看| 亚洲少妇自拍| 亚洲欧美日韩一区| 久久久精彩视频| 久久网站热最新地址| 欧美成人午夜77777| 亚洲成人直播| 午夜精品三级视频福利| 亚洲欧美另类在线观看| 久久9热精品视频| 狂野欧美一区| 亚洲激情成人| 亚洲午夜视频在线观看| 欧美一区成人| 欧美a一区二区| 国产精品国产三级国产普通话蜜臀 | 午夜精品久久久久久| 久久久久久久激情视频| 免费在线成人| 国产精品乱码妇女bbbb| 国内成+人亚洲| 亚洲靠逼com| 欧美制服丝袜| 亚洲国产精品一区二区久| 亚洲视频每日更新| 久久精品九九| 欧美日韩精品免费在线观看视频| 国产精品视频久久久| 亚洲国产欧美久久| 亚洲综合社区| 欧美肥婆bbw| 亚洲欧美亚洲| 欧美激情一区二区三区蜜桃视频 | 免费不卡在线观看av| 国产精品99免费看| 亚洲电影激情视频网站| 亚洲欧美日韩天堂| 亚洲国产成人av| 欧美在线观看www| 欧美日韩日本网| 在线日韩一区二区| 欧美在线高清| 一区二区三区高清在线观看| 久久久噜久噜久久综合| 国产九九精品视频| 在线中文字幕一区|