函数与const参数重载(跟进) [英] functions with const arguments Overloading ( Follow up)

查看:189
本文介绍了函数与const参数重载(跟进)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是上一个问题的后续跟进

它真的很复杂,所以我开始一个新的线程,使我的观点更清楚。(Didnt想删除以前的线程,因为其他家伙谁给了有价值的反馈,不要松动的声誉点他们获得了)

It got really complicated so I am starting a new thread to make my point clearer.( Didnt want to delete the previous thread because the other guys who gave valuable feedback dont not loose the reputation points they gained)

更新代码:(符合与工程)

Updated Code: (Complies and Works)

#include <iostream>
 using std::cout;

 class Test {
         public:
         Test(){ }   
         int foo (const int) const;
         int foo (int );
 };  

 int main ()
 {   
         Test obj;
         int variable=0;  
         int output;
     do{ 
         output=obj.foo(3);        // Call the const function 
         cout<<"output::"<<output<<std::endl;
         output=obj.foo(variable); // Want to make it call the non const function 
         cout<<"output::"<<output<<std::endl;
         variable++; 
             usleep (2000000);
        }while(1);  
 }   

 int Test::foo(int a)
 {   
    cout<<"NON CONST"<<std::endl;
    a++;
    return a;
 }   

 int Test::foo (const int a) const
 {   
    cout<<"CONST"<<std::endl;
    return a;
 }   

输出(我得到):

NON CONST
output::4
NON CONST
output::1
NON CONST
output::4
NON CONST
output::2
NON CONST
output::4
NON CONST
output::3
NON CONST
output::4
NON CONST
output::4
NON CONST
output::4
NON CONST
output::5

输出(我想/已经记住)

Output (I desired/had in mind)

CONST
output::3
NON CONST
output::1
CONST
output::3
NON CONST
output::2
CONST
output::3
NON CONST
output::3
CONST
output::3
NON CONST
output::4
CONST
output::3
NON CONST
output::5

希望我更好地介绍我的问题。我知道其他方法。但这是可能的。

Hope I have presented my question better. I know other ways to do it. but is this possible.

推荐答案

在C ++中,函数签名

In C++, the function signatures

int Test::foo (const int a) const

int Test::foo (int a) const

被认为是完全相同的。

const 参数被忽略是因为它不能以任何方式影响调用者。由于参数是通过值传递的,所以拷贝由调用者提供的值。对于调用者来说,如果被调用的函数可以改变该副本,那么它在任何方面都无关紧要。
因此,C ++忽略了顶层的 const - 对函数参数的限定(顶层 const 如果传递引用,则不会发生),并且 int foo(int); 被认为是函数的正确原型

The reason that the const on the parameter is disregarded is because it can not affect the caller in any way. As the parameter is passed by value, a copy is made of the value provided by the caller. To the caller, it does not matter in any way if the called function can change that copy or not. For this reason, C++ ignores a top-level const-qualification on function parameters (top-level const can not occur if passing a reference), and goes even as far that int foo(int); is considered a correct prototype for the function

int foo(const int)
{
    /* ... */
}

简而言之,在C ++中不可能重载一个函数对(value)函数参数的常量。
为了得到你想要的输出,你可以考虑对你的非const重载使用一个非const引用参数。

In short, it is impossible in C++ to overload a function on the constness of (value) function parameters. To get the output you want, you could consider using a non-const reference parameter for your non-const overload.

这篇关于函数与const参数重载(跟进)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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