65,179
社区成员




#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;
}
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;
}
这个和你那个有区别的啊,
#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;
}
在试一下啊,
#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;
}