如何避免模棱两可的构造函数? [英] How to avoid ambiguous constructors?

查看:72
本文介绍了如何避免模棱两可的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个构造函数的类,它们都使用相同类型的

参数作为参数。我应该怎么做才能消除我的课程歧义?

解决方案

" Jason Heyes" < GE ****** @ optusnet.com.au>在消息中写道

news:3f *********************** @ news.optusnet.com.a u ... < blockquote class =post_quotes>我有一个带有两个构造函数的类,它们都使用相同类型的
参数作为参数。我应该怎么做才能消除我的课程歧义?




我刚找到一种方法来修复我的模糊构造函数。您需要在其中一个构造函数中添加一个

dummy int参数。当你想调用那个

特定的构造函数时,你指定一个零参数。


Jason Heyes写道:

Jason Heyes < GE ****** @ optusnet.com.au>在消息中写道
新闻:3f *********************** @ news.optusnet.com.a u ...

我有一个带有两个构造函数的类,它们都使用相同类型的
参数作为参数。我该怎么做才能消除我的课程歧义?



我刚刚找到了一种方法来修复我的模糊构造函数。您需要将
一个伪int参数添加到其中一个构造函数中。当你想要调用那个特定的构造函数时,你指定一个零参数。




我宁愿使用枚举来使结果代码更具可读性:


班级长度

{

public:

enum Unit {Centimeter,Inch};


长度(双l,单位u =厘米);


// ...

};


但是,您需要选择(使用if或switch / case)代码

,具体取决于价值。

如果你想要两个独立的构造函数,你也可以通过一个小的

修改来实现:


类长度

{

public:

enum Centimeter_t {Centimeter};

enum Inch_t {Inch};


长度( double l,Centimeter_t =厘米);

长度(double l,Inch_t);


// ...

} ;


现在你可以写:


长度l1(3); // 3厘米,构造函数1

长度l2(5英寸); // 5英寸,构造函数2

长度l3(7,厘米); // 7厘米,构造函数1

如果省略默认值,可以强制执行单位的明确

规范,以及l1的定义变得无效。




" Jason Heyes" < GE ****** @ optusnet.com.au>在消息中写道

news:3f *********************** @ news.optusnet.com.a u ... < blockquote class =post_quotes>我有一个带有两个构造函数的类,它们都使用相同类型的
参数作为参数。我应该怎么做才能消除我的课程歧义?




如果您决定

构造函数实际上是如何不同的,那么解决方案应该实际出现。如果你不能,那么它们实际上是相同的。当你确定在什么条件下你会想要一个比另一个更好的b $ b,我想你会发现你可以简单地使用同一个,但是

发送一个参数这是一个标志或枚举,用于告诉构造函数使用该参数值调用时b / b
。我怀疑你真的不需要b $ b需要多个构造函数。


I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?

解决方案

"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f***********************@news.optusnet.com.a u...

I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?



I just found one way to fix my ambiguous constructors. You need to add a
dummy int parameter to one of the constructors. When you want to call that
particular constructor, you specify a zero argument.


Jason Heyes wrote:

"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f***********************@news.optusnet.com.a u...

I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?



I just found one way to fix my ambiguous constructors. You need to add
a dummy int parameter to one of the constructors. When you want to
call that particular constructor, you specify a zero argument.



I''d rather use an enum to make the resulting code more readable:

class Length
{
public:
enum Unit { Centimeter, Inch };

Length(double l, Unit u = Centimeter);

//...
};

However, you will need to select (with if or switch/case) the code
depending on the value.
If you want two separate constructors, you can do it too with a small
modification:

class Length
{
public:
enum Centimeter_t { Centimeter };
enum Inch_t { Inch };

Length(double l, Centimeter_t = Centimeter);
Length(double l, Inch_t);

//...
};

Now you can write:

Length l1(3); // 3 centimeters, constructor 1
Length l2(5, Inch); // 5 inches, constructor 2
Length l3(7, Centimeter); // 7 centimeters, constructor 1

If you leave out the default value, you can enforce the explicit
specification of the unit, and the definition of l1 becomes invalid.



"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f***********************@news.optusnet.com.a u...

I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?



The solution should actually present itself if you decide how the
constructors are actually different. If you can''t, then they are actually
the same. When you identify under what conditions you would want one rather
than the other, I think you''ll find you can simply use the same one, but
send a parameter that is a flag or enum to tell the constructor what to do
differently when it is called with that parameter value. I doubt you really
need more than one constructor.


这篇关于如何避免模棱两可的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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