由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请叫一个 template class constructor 的问题
相关主题
请教各路C++大神 为什么f(3) 输出是 'dd'c++ operator overloading question
a simple question about constructor请教几个C++问题
Re: VC里面的stl支持是不是很弱?one question about initializaiton list
[合集] 请问-fno-implicit-templates的用处[合集] 又学了一招
请问这是什么错误呀question regarding const function
[合集] 关于C++ Class Template编程一问c++ question
Any difference between class and typename identifier?形参可以直接使用私有数据成员?
Default function template arguments菜鸟请教smart pointer
相关话题的讨论汇总
话题: gvector话题: template话题: class话题: copy
进入Programming版参与讨论
1 (共1页)
b**n
发帖数: 289
1
我有一个GVector 的Class,它有一个copy constructor是用template的。
template
class GVector{
public:
template GVector (const GVector& rhs): length_(rhs.length()
){
ptr_ = new (nothrow) T[length_];
assert(ptr_);
std::copy(&rhs(0), &rhs(0) + length_, ptr_);
}
但我每次在下列情况下(X = T)都会出现运行期的内存错误
GVector v(3);
GVector v2(v);
但如果在X != T时,就没有问题,比如:
GVector v(3);
GVector vi(v);
请问这是怎么回事? 我现在在把我以前写的一些class改成template的,结果问题好多
啊!
谢谢帮忙。
t****t
发帖数: 6806
2
This is due to the incorrect use of terminology.
"它有一个copy constructor是用template的。"
1. "Copy constructor" means the special, non-template constructor, if the
first parameter is of type X&, const X&, volatile X&, or const volatile X&(,
and other parameter can be omitted). Therefore, if the parameter is of type
T& (T!=X), it is not a copy constructor; if a constructor is a template, it
is not a copy constructor. Repeat: copy constructor is never a template.
2. Copy constructor is implicitly define

【在 b**n 的大作中提到】
: 我有一个GVector 的Class,它有一个copy constructor是用template的。
: template
: class GVector{
: public:
: template GVector (const GVector& rhs): length_(rhs.length()
: ){
: ptr_ = new (nothrow) T[length_];
: assert(ptr_);
: std::copy(&rhs(0), &rhs(0) + length_, ptr_);
: }

b**n
发帖数: 289
3
This is really helpful. First time to hear this. Thank you very much.
1 (共1页)
进入Programming版参与讨论
相关主题
菜鸟请教smart pointer请问这是什么错误呀
[合集] 关于template和inheritance的问题请教[合集] 关于C++ Class Template编程一问
一个C++ template的问题Any difference between class and typename identifier?
which func will be called?Default function template arguments
请教各路C++大神 为什么f(3) 输出是 'dd'c++ operator overloading question
a simple question about constructor请教几个C++问题
Re: VC里面的stl支持是不是很弱?one question about initializaiton list
[合集] 请问-fno-implicit-templates的用处[合集] 又学了一招
相关话题的讨论汇总
话题: gvector话题: template话题: class话题: copy