高手请进--如何写模板类的拷贝构造函数

jxnczyp 2007-12-09 09:40:09
#include<iostream>
#include<vector>
#include<string>


using namespace std;

template <typename T,size_t size>
class fixed_vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
fixed_vector()
{
cout<<"默认构造函数"<<endl;
}


template<typename T,size_t osize>
fixed_vector( const fixed_vector<T,osize>& other)
{
cout<<"模板拷贝构造函数"<<endl;
}
template <typename T,size_t osize>
fixed_vector<T,size>&
operator=(const fixed_vector<T,osize>& other)
{
cout<<"模板赋值函数"<<endl;//这不是真的赋值函数
return *this;
}

iterator begin()
{
return m_v;
}
iterator end()
{
return m_v+size;
}

const_iterator begin() const
{
return m_v;
}

const_iterator end() const
{
return m_v+size;
}

private:
T m_v[size];
};


int main()
{
fixed_vector<char,4> fv_char;
fixed_vector<int,4> fc_int1;
fixed_vector<int,8> fc_int2(fc_int1);//这里将调用模板拷贝构造函数
fc_int2=fc_int1;//这里将调用模板赋值函数

fixed_vector<int,4> fc_int3;
fixed_vector<int,4> fc_int4(fc_int3);//这里不会调用模板拷贝构造函数
fc_int3=fc_int4;//不调用模板赋值函数

system("pause");
return 0;
}
...全文
640 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxnczyp 2007-12-09
  • 打赏
  • 举报
回复
楼上写得好像不能通过编译,那里有错吗??
ryfdizuo 2007-12-09
  • 打赏
  • 举报
回复

fixed_vector <int,4> fc_int3; //第n行
fixed_vector <int,4> fc_int4(fc_int3);//第n+1行
此时肯定不会调用你写的那个构造函数的啊,他会优先的调用默认的构造函数:
fixed_vector&
operator=(const fixed_vector& other)
{
int i=0;
for(const_iterator bg=other.begin(); bg!=other.end()&&i<size; bg++, i++)
m_v[i]=*bg;
cout<<"模板赋值函数--2"<<endl;
return *this;
}
这个和你那个有区别的啊,
jxnczyp 2007-12-09
  • 打赏
  • 举报
回复
怪我没说清楚,请看下面
fixed_vector<int,4> fc_int3; //第n行
fixed_vector<int,4> fc_int4(fc_int3);//第n+1行
你在main()函数里面加入这两行,主程序根本不会调用我写的那个函数即下在:
template<typename T,size_t osize>
fixed_vector( const fixed_vector<T,osize>& other)
{
}

执行n+1行时不会调用上面这个函数
ryfdizuo 2007-12-09
  • 打赏
  • 举报
回复

#include<iostream>
#include<vector>
#include<string>
using namespace std;


template <typename T, size_t size>
class fixed_vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
fixed_vector()
{
cout<<"默认构造函数"<<endl;
}


template<typename T,size_t osize>
fixed_vector( const fixed_vector<T,osize>& other )
{
int i=0;
for(const_iterator bg=other.begin(); bg!=other.end()&&i<size; bg++, i++)
m_v[i]=*bg;
cout<<"模板拷贝构造函数--1"<<endl;
}

fixed_vector( const fixed_vector& other )
{
int i=0;
for(const_iterator bg=other.begin(); bg!=other.end()&&i<size; bg++, i++)
m_v[i]=*bg;
cout<<"模板拷贝构造函数--2"<<endl;
}

template <typename T,size_t osize>
fixed_vector<T,size>&
operator=(const fixed_vector<T,osize>& other)
{
int i=0;
for(const_iterator bg=other.begin(); bg!=other.end()&&i<size; bg++, i++)
m_v[i]=*bg;
cout<<"模板赋值函数--1"<<endl;
return *this;
}
fixed_vector&
operator=(const fixed_vector& other)
{
int i=0;
for(const_iterator bg=other.begin(); bg!=other.end()&&i<size; bg++, i++)
m_v[i]=*bg;
cout<<"模板赋值函数--2"<<endl;
return *this;
}

iterator begin()
{
return m_v;
}
iterator end()
{
return m_v+size;
}

const_iterator begin() const
{
return m_v;
}

const_iterator end() const
{
return m_v+size;
}

private:
T m_v[size];
};


int main()
{
fixed_vector<char,4> fv_char;
fixed_vector<int,4> fc_int1;
fixed_vector<int,8> fc_int2(fc_int1);//这里将调用模板拷贝构造函数
fc_int2=fc_int1;//这里将调用模板赋值函数

//fixed_vector<int,8> fc_int3;
fixed_vector<int,4> fc_int3; //此时就会调用拷贝构造函数了。因为你定义的时候是假设二者大小不一样
fixed_vector<int,4> fc_int4(fc_int3);//这里不会调用模板拷贝构造函数
fc_int3=fc_int4;//不调用模板赋值函数

system("pause");
return 0;
}
在试一下啊,
ryfdizuo 2007-12-09
  • 打赏
  • 举报
回复

#include<iostream>
#include<vector>
#include<string>
using namespace std;


template <typename T, size_t size>
class fixed_vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
fixed_vector()
{
cout<<"默认构造函数"<<endl;
}


template<typename T,size_t osize>
fixed_vector( const fixed_vector<T,osize>& other )
{
int i=0;
for(const_iterator bg=other.begin(); bg!=other.end()&&i<size; bg++, i++)
m_v[i]=*bg;
cout<<"模板拷贝构造函数"<<endl;
}
template <typename T,size_t osize>
fixed_vector<T,size>&
operator=(const fixed_vector<T,osize>& other)
{
int i=0;
for(const_iterator bg=other.begin(); bg!=other.end()&&i<size; bg++, i++)
m_v[i]=*bg;
cout<<"模板赋值函数"<<endl;//这不是真的赋值函数
return *this;
}

iterator begin()
{
return m_v;
}
iterator end()
{
return m_v+size;
}

const_iterator begin() const
{
return m_v;
}

const_iterator end() const
{
return m_v+size;
}

private:
T m_v[size];
};


int main()
{
fixed_vector<char,4> fv_char;
fixed_vector<int,4> fc_int1;
fixed_vector<int,8> fc_int2(fc_int1);//这里将调用模板拷贝构造函数
fc_int2=fc_int1;//这里将调用模板赋值函数

//fixed_vector<int,4> fc_int3;
fixed_vector<int,8> fc_int3; //此时就会调用拷贝构造函数了。因为你定义的时候是假设二者大小不一样
fixed_vector<int,4> fc_int4(fc_int3);//这里不会调用模板拷贝构造函数
fc_int3=fc_int4;//不调用模板赋值函数

system("pause");
return 0;
}

jxnczyp 2007-12-09
  • 打赏
  • 举报
回复
这说明我上面写的那个模板拷贝构造函数不是真正的模板拷贝构造函数,那真正的模板拷贝函数函数该怎么写呢??

65,179

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧