模板和复制构造函数 [英] Template and Copy constructor
问题描述
一点点样品:
A级
{
公开:
A ()
{
}
模板< typename T> A(const typename T& a)
{
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO:将代码放在这里。
A a; //这称为默认构造函数
A b(a); //这叫做默认的复制构造函数而不是我的!!!
A c(3); //这叫我的模板构造函数
A d(3.0f); //这叫我的模板构造函数
返回0;
}
为什么默认的复制构造函数被调用而不是我的我正在使用模板构造函数?编译正常,但默认构造函数不是创建A b(a)时使用的好构造函数;那里有什么不对吗?
感谢您的帮助!
Just a little sample :
class A
{
public:
A( )
{
}
template<typename T> A( const typename T& a)
{
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
A a; // This called the default constructor
A b( a ); // This called the default Copy constructor and not mine !!!
A c( 3 ); // This called my template constructor
A d( 3.0f ); // This called my template constructor
return 0;
}
Why the default copy constructor is called instead of mine when I''m using template constructor ? This compile fine but the default constructor is not the good one used when creating A b( a ); Is there something wrong there ?
Thanks for help !
推荐答案
MurphyII写道:
MurphyII wrote:
只是一个小样本:
A级
{
公开:
A()
{
}
模板< typename T> A(const typename T& a)
{
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
// TODO:将代码放在这里。
A a; //这称为默认构造函数
A b(a); //这叫默认的复制构造函数而不是我的!!! A c(3); //这叫我的模板构造函数
A d(3.0f); //这叫我的模板构造函数
返回0;
}
为什么在我使用模板构造函数时调用默认的复制构造函数而不是我的?编译正常,但默认的
构造函数不是创建A b(a)时使用的好构造函数;那里有什么问题吗?感谢您的帮助!
Just a little sample :
class A
{
public:
A( )
{
}
template<typename T> A( const typename T& a)
{
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
A a; // This called the default constructor
A b( a ); // This called the default Copy constructor and not
mine !!! A c( 3 ); // This called my template constructor
A d( 3.0f ); // This called my template constructor
return 0;
}
Why the default copy constructor is called instead of mine when I''m
using template constructor ? This compile fine but the default
constructor is not the good one used when creating A b( a ); Is there
something wrong there ? Thanks for help !
没有任何错误,而是符合C ++标准。
模板化构造函数永远不会被考虑复制构造函数即使
它也是参数类型的完美匹配。你需要提供你自己的副本
构造函数,如果编译提供的那个不适合你的课程。
-cd
There is nothing wrong, rather this is in accordance with the C++ Standard.
A templated constructor will never be considered a copy constructor even if
it''s a perfect match on argument types. You need to supply your own copy
constructor if the compiled-supplied one is inadequate for your class.
-cd
但没有警告......这不是我们期待的行为,我想没人知道......好的,谢谢你的帮助!
But there is no warning... It''s not really the behaviour we are expecting and I think nobody knows it... Ok, thanks for your help !
MurphyII写道:
MurphyII wrote:
但是没有警告......这不是我们期待的行为,我想没人知道......好的,谢谢你的帮助!
But there is no warning... It''s not really the behaviour we are
expecting and I think nobody knows it... Ok, thanks for your help !
这是一个非常常见的陷阱 - 一直出现在新闻组中。
类似的情况也适用于复制赋值运算符。
FYI:C ++标准,第12.8节,脚注106说
因为模板构造函数永远不是复制构造函数,所以
这样的模板的存在并不会抑制复制
构造函数的隐式声明。模板构造函数参与重载解析与
其他构造函数,包括复制构造函数和模板构造函数
可用于复制对象,如果它提供的匹配比其他
构造函数。
-cd
It''s a pretty common trap - comes up on the newsgroups all the time.
An analogous case applies to the copy-assignment operator as well.
FYI: C++ Standard, section 12.8, footnote 106 says
"Because a template constructor is never a copy constructor, the presence of
such a template does not suppress the implicit declaration of a copy
constructor. Template constructors participate in overload resolution with
other constructors, including copy constructors, and a template constructor
may be used to copy an object if it provides a better match than other
constructors."
-cd
这篇关于模板和复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!