f(const MyClass& in)和f(MyClass in)之间有什么区别 [英] What's difference between f(const MyClass & in) and f(MyClass in)

查看:73
本文介绍了f(const MyClass& in)和f(MyClass in)之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义以下代码:


void f(const MyClass& in){cout<< " f(const)\ n";}

void f(MyClass in){cout<<" f()\ n";}


MyClass myclass;

f(myclass);


编译器抱怨它无法找到最佳匹配。任何人都可以在理论上给出一个

的详细解释?哪一个好?


谢谢

If I define the following codes:

void f(const MyClass & in) {cout << "f(const)\n";}
void f(MyClass in) {cout<<"f()\n";}

MyClass myclass;
f(myclass);

Compiler complain that it can''t find the best match. Anyone could give a
detail explanation in theory? Which one is good?

Thanks

推荐答案



modemer写道:

modemer wrote:
如果我定义以下代码:

void f(const MyClass& in){cout<< " f(const)\ n";}
void f(MyClass in){cout<<" f()\ n";}

MyClass myclass;
f(myclass);

编译器抱怨它无法找到最佳匹配。任何人都可以
在理论上给出详细解释?哪一个好?
If I define the following codes:

void f(const MyClass & in) {cout << "f(const)\n";}
void f(MyClass in) {cout<<"f()\n";}

MyClass myclass;
f(myclass);

Compiler complain that it can''t find the best match. Anyone could give a detail explanation in theory? Which one is good?



那么,您认为*应该被调用哪个函数?答案是

没人知道。它可以调用任何一个,这就是为什么编译器

抱怨它没有最佳的原因。匹配,因为两场比赛都是相同的。


希望这会有所帮助,

-shez-


Well, which function do you think *should* get called? The answer is
"nobody knows". It could call either one, that''s why the compiler
complains that it doesn''t have a "best" match, because both matches are
the same.

Hope this helps,
-shez-


谢谢Shezan,你说的是我发现的,这就是为什么我在这里问这个

的问题,但我想知道是否有是关于C ++理论的任何讨论

关于这个?我不擅长阅读C ++理论书:-)


如果我说,这2个功能有相同的签名,我认为这是错的,因为

编译器应该抱怨功能已被定义。

如果我说,这2个功能超载,我认为它也不正确因为

他们我认为C ++理论可以讨论这种情况并解释原因。

我不确定我的问题是否足够明确。
/>

谢谢。

" Shezan Baig" < SH ************ @ gmail.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...
Thanks Shezan, what you said is what I found and that''s why I asked this
question here, but I want to know if there is any discussion in C++ theory
about this? I am not good at reading C++ theory book :-)

if I say, these 2 funcs have same signature, I think it''s wrong because
compiler should complain like "function has been defined".
if I say, these 2 funcs is overloaded, I think it''s also not correct because
they are conflicting.
so I think C++ theory could discuss this situation and explain the reason.

I am not sure if my question is clear enough.

Thanks.
"Shezan Baig" <sh************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

modemer写道:

modemer wrote:
如果我定义以下代码:

void f(const MyClass& in){cout<< " f(const)\ n";}
void f(MyClass in){cout<<" f()\ n";}

MyClass myclass;
f(myclass);

编译器抱怨它无法找到最佳匹配。任何人都可以
If I define the following codes:

void f(const MyClass & in) {cout << "f(const)\n";}
void f(MyClass in) {cout<<"f()\n";}

MyClass myclass;
f(myclass);

Compiler complain that it can''t find the best match. Anyone could


在理论上给出


give a

详细解释?哪一个好?



那么,您认为*应该被调用哪个函数?答案是没人知道。它可以调用任何一个,这就是编译器抱怨它没有最佳的原因。匹配,因为两场比赛都是一样的。

希望这会有所帮助,
-shez -


Well, which function do you think *should* get called? The answer is
"nobody knows". It could call either one, that''s why the compiler
complains that it doesn''t have a "best" match, because both matches are
the same.

Hope this helps,
-shez-





" modemer" < me@privacy.net.invalid>在消息中写道

news:d1 ********** @ domitilla.aioe.org ...

"modemer" <me@privacy.net.invalid> wrote in message
news:d1**********@domitilla.aioe.org...
如果我定义以下代码:

void f(const MyClass& in){cout<< " f(const)\ n";}
void f(MyClass in){cout<<" f()\ n";}

MyClass myclass;
f(myclass);

编译器抱怨它无法找到最佳匹配。任何人都可以在理论上给出详细解释?哪一个好?

谢谢
If I define the following codes:

void f(const MyClass & in) {cout << "f(const)\n";}
void f(MyClass in) {cout<<"f()\n";}

MyClass myclass;
f(myclass);

Compiler complain that it can''t find the best match. Anyone could give a
detail explanation in theory? Which one is good?

Thanks



问题是调用它们的语法是一样的。鉴于

第一个函数,它接受一个const引用,你通过传递一个

类的实例来调用它。给定第二个函数,它按值得到它的参数

(制作副本就像这样),你也可以传递一个

类的实例。那么,当编译器看到你通过传递一个实例来调用函数

时,应该调用哪个函数?


我从未见过一个实例我需要有两个版本的这种

功能。为什么你们两个都需要它们?


如果他们做不同的事情,那么也许他们应该有不同的

名字......?


如果他们做同样的事情,那么为什么要有两个呢?选择你喜欢的
并删除另一个。


我也想知道为什么编译器允许你声明两个函数

后来会导致无法选择最佳匹配。也许有一种

的方式构建一个看起来不会调用

其他的电话?我不知道副手,但正如我所说,我根本就不这样做,

因此问题永远不会出现。


至于哪一个最好,它可能也可能不重要。如果你有一个

的对象复制成本很高,那么你可能更喜欢通过const引用,

,因为它没有复制。但是,如果你在蓝月亮中只调用一次这个函数,那么整体性能成本在实践中可以忽略不计,即使每个实例的代价很高,也需要
基础。


我倾向于选择const引用,作为一种习惯,除非我通过
传递内置类型,我通过值传递的情况。但那只是

我。


-Howard



The problem is that the syntax for calling those is the same. Given the
first function, which takes a const reference, you call it by passing an
instance of the class. Given the second function, which gets its parameter
by value (making a copy as it does so), you also cll it by passing an
instance of the class. So, when the compiler sees you calling the function
by passing an instance, which function should be called?

I''ve never seen an instance where I need to have both versions of such a
function available. Why do you need them both?

If they do different things, then perhaps they should have different
names...?

If they do the same exact thing, then why have two of them? Pick whichever
you prefer and remove the other one.

I also wonder why the compiler allows you to declare two functions which
will later lead to an inability to pick the best match. Perhaps there is a
way to construct a call to one that will not appear to be a call to the
other? I don''t know off-hand, but as I said, I simply don''t do that anyway,
so the issue never comes up.

As for which is best, it may or may not matter. If you have an object for
which copying is costly, then you may prefer to pass by const reference,
since it does not make a copy. However, if you only call this function once
in a blue moon, the overall performance cost may be negligible in practice,
even if it is costly on a per-instance basis.

I tend to prefer the const reference, as a habit mostly, unless what I''m
passing is a built-in type, in which case I pass by value. But that''s just
me.

-Howard



这篇关于f(const MyClass&amp; in)和f(MyClass in)之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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