类模板的拷贝构造函数如何定义(VLine(const VLine& VL); OR VLine(const VLine& VL);)

romberg2002 2003-06-22 09:41:50
template<class T>
class VLine
{
public:
VLine();
VLine(T X1, T X2, T Y1, T Y2);

VLine(const VLine& VL);///这样定义行吗?感觉有问题!
///VLine(const VLine<T>& VL);///这个呢?

///采用下面的定义可以,但这对类模板太具讽刺意义了吧?
/*******************************************\
//VLine(const VLine<int>& VL);///For int
//VLine(const VLine<float>&VL);///For float
//VLine(const VLine<double>& VL);///For double
...
\********************************************/
......
};
...全文
141 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
romberg2002 2003-06-22
  • 打赏
  • 举报
回复
谢谢楼上的回复,vector、list的例子我也看过了,但我想实现的功能是:可以用vector<int>构造vector<float>或别的类型,例如:
vector<int> IntV;
IntV.clear();
IntV.push_back(0);
vector<float> FloatV(IntV);
但编译的时候会有错误(这是一定的),而如果我这样定义我的类模板,就不会有错误:
template<class T>
class VLine
{
public:
VLine();
VLine(T X1, T X2, T Y1, T Y2);

VLine(const VLine<int>& VL);///For int
VLine(const VLine<float>&VL);///For float
VLine(const VLine<double>& VL);///For double
...
......
};

可以这样用上面的类:
VLine<int> IntVLine(0, 0, 0, 0);
VLine<float> FloatVLine(IntVLine);//Ok
跟踪一下代码会发现在运行的时候编译器会选择合适的拷贝构造函数!

相关代码如下:
#include <iostream>
using namespace std;

template <class T>
class VLine
{
public:
VLine():m_X1(0),m_Y1(0),m_X2(0),m_Y2(0)
{ }

VLine(T X1, T X2, T Y1, T Y2):m_X1(X1),m_Y1(Y1),m_X2(X2),m_Y2(Y2)
{ }

VLine(const VLine<int>& VL)
{
cout<<"Int Copy Constuctor!"<<endl;
m_X1 = static_cast<T>(VL.GetX1());
m_X2 = static_cast<T>(VL.GetX2());
m_Y1 = static_cast<T>(VL.GetY1());
m_Y2 = static_cast<T>(VL.GetY2());
}

VLine(const VLine<float>& VL)
{
cout<<"Float Copy Constuctor!"<<endl;
m_X1 = static_cast<T>(VL.GetX1());
m_X2 = static_cast<T>(VL.GetX2());
m_Y1 = static_cast<T>(VL.GetY1());
m_Y2 = static_cast<T>(VL.GetY2());
}

T GetX1() const {return m_X1;}

T GetX2() const {return m_X2;}

T GetY1() const {return m_Y1;}

T GetY2() const {return m_Y2;}

void SetValue(const T& X1, const T& Y1, const T& X2, const T& Y2)
{
m_X1 = X1;
m_Y1 = Y1;
m_X2 = X2;
m_Y2 = Y2;
}

private:
T m_X1;
T m_Y1;
T m_X2;
T m_Y2;
};

main()
{
VLine<int> IntVL(1, 0, 0, 0);

cout<<IntVL.GetX1()<<endl;
cout<<IntVL.GetY1()<<endl;
cout<<IntVL.GetX2()<<endl;
cout<<IntVL.GetY2()<<endl<<endl;

VLine<float> FloatVL(IntVL);

cout<<FloatVL.GetX1()<<endl;
cout<<FloatVL.GetY1()<<endl;
cout<<FloatVL.GetX2()<<endl;
cout<<FloatVL.GetY2()<<endl<<endl;

FloatVL.SetValue(1, 2, 3, 4);

VLine<int> IntVL2(FloatVL);

cout<<IntVL2.GetX1()<<endl;
cout<<IntVL2.GetY1()<<endl;
cout<<IntVL2.GetX2()<<endl;
cout<<IntVL2.GetY2()<<endl<<endl;

return 0;
}
snipersu 2003-06-22
  • 打赏
  • 举报
回复
VLine(const VLine<T>& VL);
  • 打赏
  • 举报
回复
template<class _Ty, class _A = allocator<_Ty> >
class vector {
public:
typedef vector<_Ty, _A> _Myt; //!!
typedef _A allocator_type;
typedef _A::size_type size_type;
typedef _A::difference_type difference_type;
typedef _A::pointer _Tptr;
typedef _A::const_pointer _Ctptr;
typedef _A::reference reference;
typedef _A::const_reference const_reference;
typedef _A::value_type value_type;
typedef _Tptr iterator;
typedef _Ctptr const_iterator;
typedef reverse_iterator<const_iterator, value_type,
const_reference, _Ctptr, difference_type>
const_reverse_iterator;
typedef reverse_iterator<iterator, value_type,
reference, _Tptr, difference_type>
reverse_iterator;
explicit vector(const _A& _Al = _A())
: allocator(_Al), _First(0), _Last(0), _End(0) {}
explicit vector(size_type _N, const _Ty& _V = _Ty(),
const _A& _Al = _A())
: allocator(_Al)
{_First = allocator.allocate(_N, (void *)0);
_Ufill(_First, _N, _V);
_Last = _First + _N;
_End = _Last; }
vector(const _Myt& _X) //!
romberg2002 2003-06-22
  • 打赏
  • 举报
回复
谢谢micropentium6(小笨) ,呵呵。
另外希望大家对上面那个程序作一些测试,看程序的行为是否正确,如发现异常,希望能告诉我一下,我好作相应修改。
明天结帖吧。
  • 打赏
  • 举报
回复
VC中好像不行 DEV C++可以
你看看这个,是chinajiji写的,好久没看到他了:)
http://expert.csdn.net/Expert/topic/1585/1585555.xml?temp=.8479273
romberg2002 2003-06-22
  • 打赏
  • 举报
回复
不好意思,现在还有个问题:
怎么样才能把这个“模板类的模板拷贝构造函数”的定义写在类声明之外,即不把这个函数定义成内联函数?
谢谢各位
romberg2002 2003-06-22
  • 打赏
  • 举报
回复
明确一下我的问题:有没有类似模板函数一样的表述方式可以将拷贝构造函数只写一次就能应对各种情况,这样一来我就没必要针对不同的数据类型些不同的构造函数了。
类似:
template<class ValueType>
VLine(const VLine<ValueType>& VL)
{
cout<<"Template Copy Constuctor!"<<endl;
m_X1 = static_cast<T>(VL.GetX1());
m_X2 = static_cast<T>(VL.GetX2());
m_Y1 = static_cast<T>(VL.GetY1());
m_Y2 = static_cast<T>(VL.GetY2());
}

我的天,写完上面这个函数,突然想到在VC里面试了一下,居然可以正常使用!这个问题就这样解决了。楼上各位接分。

更改后的程序如下:


#include <iostream>
using namespace std;
template <class T>
class VLine
{
public:
VLine():m_X1(0),m_Y1(0),m_X2(0),m_Y2(0)
{ }

VLine(T X1, T X2, T Y1, T Y2):m_X1(X1),m_Y1(Y1),m_X2(X2),m_Y2(Y2)
{ }

///就是这个模板类的模板拷贝构造函数解决了这个问题
template<class ValueType>
VLine(const VLine<ValueType>& VL)
{
cout<<"Template Copy Constuctor!"<<endl;
m_X1 = static_cast<T>(VL.GetX1());
m_X2 = static_cast<T>(VL.GetX2());
m_Y1 = static_cast<T>(VL.GetY1());
m_Y2 = static_cast<T>(VL.GetY2());
}

T GetX1() const {return m_X1;}

T GetX2() const {return m_X2;}

T GetY1() const {return m_Y1;}

T GetY2() const {return m_Y2;}

void SetValue(const T& X1, const T& Y1, const T& X2, const T& Y2)
{
m_X1 = X1;
m_Y1 = Y1;
m_X2 = X2;
m_Y2 = Y2;
}

private:
T m_X1;
T m_Y1;
T m_X2;
T m_Y2;
};

main()
{
VLine<int> IntVL(1, 0, 0, 0);

cout<<IntVL.GetX1()<<endl;
cout<<IntVL.GetY1()<<endl;
cout<<IntVL.GetX2()<<endl;
cout<<IntVL.GetY2()<<endl<<endl;

VLine<float> FloatVL(IntVL);

cout<<FloatVL.GetX1()<<endl;
cout<<FloatVL.GetY1()<<endl;
cout<<FloatVL.GetX2()<<endl;
cout<<FloatVL.GetY2()<<endl<<endl;

FloatVL.SetValue(1, 2, 3, 4);

VLine<int> IntVL2(FloatVL);

cout<<IntVL2.GetX1()<<endl;
cout<<IntVL2.GetY1()<<endl;
cout<<IntVL2.GetX2()<<endl;
cout<<IntVL2.GetY2()<<endl<<endl;

return 0;
}


magic007 2003-06-22
  • 打赏
  • 举报
回复
vector<float> FloatV(IntV);
显然需要调用vector<float>的copy constructor,但标准vector类中并没有定义不同模板参数之间的copy constructor,当然不行了。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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