模板类的拷贝构造函数和重载=

http://bbs.csdn.net/topics/190144045

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4
 5
 6 using namespace std;
 7
 8 template <typename T,size_t size>
 9 class fixed_vector
10 {
11     public:
12         typedef T* iterator;
13         typedef const T* const_iterator;
14         fixed_vector()
15         {
16             cout<<"默认构造函数"<<endl;
17         }
18
19
20         template<typename T2,size_t osize>  //如果模板函数的T和类的T可能不是一个类型的话,那么两个T要命名成不同的名字  (否则在linux下编译不成功. 在vs2010下可以同名)
21             fixed_vector( const fixed_vector<T2,osize>& other)
22             {
23                 cout<<"模板拷贝构造函数1"<<endl;
24             }
25
26         fixed_vector( const fixed_vector& other)
27         {
28                 cout<<"模板拷贝构造函数2"<<endl;
29         }
30
31         template <typename T2,size_t osize>
32             fixed_vector<T2,size>& operator=(const fixed_vector<T2,osize>& other) //注意这里返回的是size而不是osize
33             {
34                 cout<<"模板赋值函数1"<<endl;//这不是真的赋值函数
35                 return *this;
36             }
37
38         fixed_vector<T,size>& operator=(const fixed_vector& other) //注意这里返回的是size而不是osize
39         {
40             cout<<"模板赋值函数2"<<endl;//这不是真的赋值函数
41             return *this;
42         }
43
44         iterator begin()
45         {
46             return m_v;
47         }
48         iterator end()
49         {
50             return m_v+size;
51         }
52
53         const_iterator begin() const
54         {
55             return m_v;
56         }
57
58         const_iterator end() const
59         {
60             return m_v+size;
61         }
62
63     private:
64         T m_v[size];
65 };
66
67
68 int main()
69 {
70     cout<<"--------begin <char, 4>----------"<<endl;
71     fixed_vector<char,4> fv_char;
72     cout<<"--------begin <int , 4>----------"<<endl;
73     fixed_vector<int,4> fc_int1;
74     cout<<"--------begin <int, 8>(<int , 4>)----------"<<endl;
75     fixed_vector<int,8> fc_int2(fc_int1);//这里将调用模板拷贝构造函数
76     cout<<"--------begin <int, 8> = <int , 4>----------"<<endl;
77     fc_int2=fc_int1;//这里将调用模板赋值函数
78
79     cout<<"--------begin <int , 4>----------"<<endl;
80     fixed_vector<int,4> fc_int3;
81     cout<<"--------begin <int, 4>(<int , 4>)----------"<<endl;
82     fixed_vector<int,4> fc_int4(fc_int3);//这里不会调用模板拷贝构造函数
83     cout<<"--------begin <int, 4> = <int , 4>----------"<<endl;
84     fc_int3=fc_int4;//不调用模板赋值函数
85
86     //system("pause");
87     return 0;
88 }

---------------------------------------------------------------------------------------------------------------------------

http://blog.chinaunix.net/uid-7448695-id-2626460.html

昨天看《Exceptional C++》,发现一个从来没有注意到标准(C++ 标准 12.8/2,note 4):“模板构造函数永远都不能成为拷贝构造函数”。所以模板构造函数永远不能取代拷贝构造函数,即便有了模板构造函数,默认拷贝构造函数还是会合成的。

  例如:


class A
{
public:
  // 这个构造函数不会掩盖默认拷贝构造函数
  template <T>
  A (const T& t) {}
};

  同样的情况对于 operator=() 一样适合。也就是模板赋值函数不会覆盖默认拷贝赋值函数。

[美] Sutter, Hurb 著;聂雪军 译。《Exceptional C++ 中文版》。北京:机械工业出版社,2007 年 1 月第 1 版。第 11 页。

时间: 2024-07-31 15:56:05

模板类的拷贝构造函数和重载=的相关文章

初学C++-----------------类的拷贝构造函数

拷贝构造函数能够使类具有自行赋值本类对象的能力,即可生成一个对象的副本,它只以本类对象的引用作为其唯一的形参,该函数的定义形式如下: class  类名 { public: 类名(形参)://(构造函数) 类名(类名  &对象名)://(拷贝构造函数) .................. }; 类名::类名(类名  &对象名)//拷贝构造函数的实现 { 函数体 } 拷贝构造函数举例: class Point { public: Point(int xx=0,int yy=0)   //构

C++ 拷贝构造函数和重载赋值操作符相互调用分析 [转]

结论: 从面相对象编程的角度考虑,拷贝构造函数调用重载赋值操作符,重载赋值操作符调用拷贝构造函数的写法都是没有意义的.应该避免. Don't try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function that both call. ——Effective C++ Third Edition By Scott M

拷贝构造函数的重载

形式:Class_name(const Class_name & other){};//他接受一个指向类对象的常量应用作为参数.  const关键字的作用是保护other对象中的内容不发生变化. 1.何时调用拷贝构造函数: 新建一个对象并将其初始化为同类现有对象时,拷贝构造函数都将被调用. StringBad ditto(motto); StringBad metoo = motto; StringBad also = StringBad(motto); StringBad * pStringB

C++ 模板类友元之输出流操作符重载

几个关键点: 需要前置声明!--奇怪的是别人告诉我也可以不需要,但我这里不行! 友元函数的函数名后面的<>,必须要有. #include <stdio.h> #include <iostream> using namespace std; //前置声明,你妹啊 template<class T> class A; template<class T> ostream &operator<< (ostream &out,

第五篇:明确拒绝不想编译器自动生成的拷贝构造函数和赋值运算符重载函数

前言 如果你不想要编译器帮你自动生成的拷贝机制 (参考前文),那么你应当明确的拒绝. 如何拒绝?这便是本文要解决的主要问题. 问题描述 当你定义了一个类,而这个类中各对象之间也是封装的 - 禁止同类对象之间的相互赋值以及复制,那么你需要屏蔽掉编译器帮你生成的拷贝构造函数以及赋值运算符. 在许多代码中,会看到通过将拷贝构造函数和赋值运算符重载函数声明为私有且不予实现来实现这个功能.然而,这不是最科学的做法. 因为这没有做到真正的屏蔽:你在自己的成员函数中,或者友元函数中仍然可以调用这两个私有函数,

明确拒绝不想编译器自动生成的拷贝构造函数和赋值运算符重载函数

前言 如果你不想要编译器帮你自动生成的拷贝机制 (参考前文),那么你应当明确的拒绝. 如何拒绝?这便是本文要解决的主要问题. 问题描述 当你定义了一个类,而这个类中各对象之间也是封装的 - 禁止同类对象之间的相互赋值以及复制,那么你需要屏蔽掉编译器帮你生成的拷贝构造函数以及赋值运算符. 在许多代码中,会看到通过将拷贝构造函数和赋值运算符重载函数声明为私有且不予实现来实现这个功能.然而,这不是最科学的做法. 因为这没有做到真正的屏蔽:你在自己的成员函数中,或者友元函数中仍然可以调用这两个私有函数,

c++拷贝构造函数、赋值运算符=重载、深拷贝与浅拷贝

 关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符 摘要: 在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作. 如果对象在申明的同时马上进行的初始化操作,则称之为拷贝运算.例如: class1 A("af"); class1 B=A; 此时其实际调用的是B(A)这样的浅拷贝操作. 如果对象在申明之后,在进行的赋值运算,我们称之为赋值运算.例如: class1 A("af"); class1 B; B=A; 此时实际调用的类

拷贝构造函数与赋值运算符重载函数要点

拷贝构造函数 一个小例子 最近在<剑指Offer>上看到了一道题(程序如下),要求我们分析编译运行的结果,并提供3个选项: A. 编译错误: B. 编译成功,运行时程序崩溃:C. 编译运行正常,输出10. 1 #include <iostream> 2 using namespace std; 3 4 class A 5 { 6 private: 7 int value; 8 9 public: 10 A(int n) { value = n; } 11 A(A other) {

拷贝构造函数(三)——重载赋值运算符

拷贝构造函数(一)--哲学三连:http://www.cnblogs.com/tenjl-exv/p/8017814.html 拷贝构造函数(二)--深拷贝与浅拷贝:http://www.cnblogs.com/tenjl-exv/p/8017909.html 拷贝构造函数(三)--重载赋值运算符:http://www.cnblogs.com/tenjl-exv/p/8017983.html 关于拷贝函数中的赋值操作符重载  以下讨论中将用到的例子: 1 class CExample 2 { 3