复制构造函数问题 [英] Copy constructor question

查看:85
本文介绍了复制构造函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个简单的问题。如果我有这样的课程:


A级{

A();

~A();
A(A& a);

A(int i);


int myvalue;

} ;


所以有一个整数的复制构造函数:


A :: A(int i){myvalue = i;}


但是如果我还想要复制构造函数用于unsigned int,long,float,

double,long double等,我是否必须全部写出来?这些副本构造函数的所有主体完全相同:


A :: A(T& t){myvalue = t;}


或者模板可以帮助我而不必写出所有变体吗?


如果我必须处理其他不是
显然是一个''简单的标量''?例如''复杂''?这需要另一个

代码体,因为那时我需要获得真正的价值(例如):


A :: A(复杂< double>& c){myvalue = c.real();}


当然复杂变量有各种变体,比如

double,float等。 。


感谢任何提示,


Jeroen

解决方案

< blockquote> Jeroen写道:


大家好,


我有一个简单的问题。如果我有这样的课程:


A级{

A();

~A();
A(A& a);

A(int i);


int myvalue;

} ;


所以有一个整数的复制构造函数:


A :: A(int i){myvalue = i;}



不,这不是复制构造函数,只有其中一个(A(A& a),

应该是真的是A(续A& a))。它是一个构造函数。


但是如果我还想要复制构造函数用于unsigned int,long,float,

double,long double等,那么我要写出来吗?



不,如果从类型到int的自动转换则不行。如果

,则使用int构造函数。


-

Ian Collins。


Jeroen写道:


大家好,


我有一个简单的问题。如果我有这样的课程:


A级{

A();

~A();
A(A& a);

A(int i);


int myvalue;

} ;



上述类可能有缺陷,因为它有一个明确的

复制构造函数但没有复制赋值运算符。如果您有非标准的复制语义,则需要在两个地方完成。


>

所以有一个整数的复制构造函数:


A: :A(int i){myvalue = i;}



这里的术语不是copy-constructor。使用同一类的cv限定类型调用复制构造函数

。你在这里拥有的是一个隐蔽的构造函数。这是一个构造函数

可以用一个参数调用(除了同一个类)

并且没有声明为明确的。


>

但是如果我还想要复制构造函数用于unsigned int,long,float,

double,long double等,我是否必须写他们全都出局了?



如果您要做的就是分配给我,为什么你认为你需要所有这些
。来自int

的单个转换构造函数可以正常工作。


A :: A(复数< double>& c){myvalue = c.real();}



你能说专业吗?我知道你可以。


typedef< class TA(const complex< T>& c){myvalue = c.real(); }


你应该让你的东西变得正确。


Ian Collins schreef:


Jeroen写道:


>大家好,

我有一个简单的问题。如果我有一个类:

A类{
A();
~A();
A(A& a);
A(int i);

int myvalue;
};

所以有一个整数的复制构造函数:

A :: A(int i){myvalue = i;}



不,这不是复制构造函数,只有其中一个(A(A& A) a),

应该是A(续A& a))。它是一个构造函数。


>但如果我还想要复制构造函数用于unsigned int,long,float,
double,long double等,我有吗?全部写出来?



不,如果从类型到int的自动转换则不行。如果

,则使用int构造函数。



好​​的,谢谢你的回答。而且你是对的(当然),只有一个拷贝构造函数是
。非常草率的我:-)


Hi guys,

I have a simple question. If I have a class like:

class A {
A();
~A();
A(A& a);
A(int i);

int myvalue;
};

So there''s a copy constructor for integers:

A::A(int i) { myvalue = i;}

But if I also want copy constructors for unsigned int, long, float,
double, long double etc, do I have to write them all out? All bodies of
these copy constructors are exactly the same:

A::A(T& t) { myvalue = t;}

Or can templates help me without having to write out all variants?

And what if I have to deal with other classes/types which are not
clearly a ''simple scalar''? For example ''complex''? This needs another
code body because then I need to get the real value (for example):

A::A(complex<double>& c) { myvalue = c.real();}

And of course the complex variable comes in various variants like
double, float, etc...

Thanks for any hints,

Jeroen

解决方案

Jeroen wrote:

Hi guys,

I have a simple question. If I have a class like:

class A {
A();
~A();
A(A& a);
A(int i);

int myvalue;
};

So there''s a copy constructor for integers:

A::A(int i) { myvalue = i;}

No, this isn''t a copy constructor, there is only one of those (A(A& a),
which should real be A(cont A& a)). It s a constructor.

But if I also want copy constructors for unsigned int, long, float,
double, long double etc, do I have to write them all out?

No, not if there is an automatic conversion from the type to int. If
there is, the int constructor will be used.

--
Ian Collins.


Jeroen wrote:

Hi guys,

I have a simple question. If I have a class like:

class A {
A();
~A();
A(A& a);
A(int i);

int myvalue;
};

The above class is probably defective in that it has a explicit
copy constructor but no copy-assignment operator. If you have
non-standard copy semantics it needs to be done in both places.

>
So there''s a copy constructor for integers:

A::A(int i) { myvalue = i;}

The term here is NOT copy-constructor. A copy constructor is
called with a cv-qualified type of the same class. What you
have here is a "coverting constructor" that is a constructor
that can be called with one argument (other than the same class)
and is NOT declared explicit.

>
But if I also want copy constructors for unsigned int, long, float,
double, long double etc, do I have to write them all out?

If all you are going to do is assign to i, why do you think you
need all of these. The single converting constructor from int
would work fine.

A::A(complex<double>& c) { myvalue = c.real();}

Can you say specialization? I knew you could.

typedef <class TA(const complex<T>& c) { myvalue = c.real(); }

You should really make your stuff const correct.


Ian Collins schreef:

Jeroen wrote:

>Hi guys,

I have a simple question. If I have a class like:

class A {
A();
~A();
A(A& a);
A(int i);

int myvalue;
};

So there''s a copy constructor for integers:

A::A(int i) { myvalue = i;}

No, this isn''t a copy constructor, there is only one of those (A(A& a),
which should real be A(cont A& a)). It s a constructor.

>But if I also want copy constructors for unsigned int, long, float,
double, long double etc, do I have to write them all out?


No, not if there is an automatic conversion from the type to int. If
there is, the int constructor will be used.

OK, thanks for the answer. And you''re right (of course), there''s only
one copy constructor. Very sloppy of me :-)


这篇关于复制构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆