Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何强制调用const限定函数重载?_C++_Constants_Overloading_C++11_C++17 - Fatal编程技术网

C++ 如何强制调用const限定函数重载?

C++ 如何强制调用const限定函数重载?,c++,constants,overloading,c++11,c++17,C++,Constants,Overloading,C++11,C++17,我试图在类内调用const函数,但存在同名的非const函数 注意:我不能只是换名字 class MyQuestion { void fun() { cout<<"a"; } void fun()const { cout<<"b"; } void call() { fun();//<how to call fun() const? } }; 类MyQuestion { 虚无乐趣() { 主题1: 通过指向const限定

我试图在类内调用
const
函数,但存在同名的非
const
函数

注意:我不能只是换名字

class MyQuestion
{
 void fun()
 {
   cout<<"a"; 
 }

 void fun()const
 { 
   cout<<"b"; 
 }

 void call()
 {
   fun();//<how to call fun() const?
 }
};
类MyQuestion
{
虚无乐趣()
{
主题1:
通过指向
const
限定类型的指针调用该函数:

void call()
{
    static_cast<const MyQuestion*>(this)->fun();
    //          ~~~~^
}
:

选项2: 将调用函数设为
常量
-限定函数:

void call() const
//          ~~~~^
{
    fun();
}

必须在常量指针上调用函数。为此,我建议创建一个局部指针变量:

const auto *c = this;
c->fun();   // calls fun() const

fun();      // calls fun()


如果您经常需要,和/或不想使用局部变量,还可以引入一个私有helper函数,该函数返回常量this指针:

const MyQuestion *const_this() const {
    return this;
}
然后像这样调用
fun

const_this()->fun();   // calls fun() const

fun();                 // calls fun()
make_const(this)->fun();    // calls fun() const

fun();                      // calls fun()
const_this->fun();   // calls fun() const

fun();               // calls fun()


另一种选择是编写一个
make_const
函数,该函数对常量指针执行强制转换,而不需要提及类名(它基本上是对推导类型的常量指针的
静态强制转换):


为了便于讨论(我不推荐以下内容),结合上面的建议,您还可以引入一个全局宏,该宏扩展为
make_const(this)

然后像这样调用
fun

const_this()->fun();   // calls fun() const

fun();                 // calls fun()
make_const(this)->fun();    // calls fun() const

fun();                      // calls fun()
const_this->fun();   // calls fun() const

fun();               // calls fun()

如何重载call()本身。下面的实现完成了任务。我猜这就是您想要实现的

#include <iostream>
using namespace std;

class A
{
public:
    void fun() { cout << "non" << endl; }
    void fun() const { cout << "const" << endl; }

    void call() { fun(); }
    void call() const { fun(); }
};

int main()
{
    A a;
    a.call();

    const A b;
    b.call();
    return 0;
}
#包括
使用名称空间std;
甲级
{
公众:

void fun(){cout我想为已经发布的优秀解决方案添加另一个可能的解决方案

您可以使用具有预期签名的函数指针帮助编译器选择正确的重载:

// Pointer to the version of fun with const
void (MyQuestion::*f)()const = &MyQuestion::fun;
(this->*f)(); // This would call the const fun
请参阅演示或下面的完整代码:

struct MyQuestion
{
    void fun()
    {
        std::cout<<"a"; 
    }

    void fun()const
    { 
        std::cout<<"b"; 
    }

    void call()
    {
        void (MyQuestion::*f)()const = &MyQuestion::fun;
        (this->*f)();
    }
};
struct MyQuestion
{
虚无乐趣()
{

std::coutIt将根据
*的常量自动调用一个或另一个
点这是一个不完全重复的问题的相关答案:您需要调用函数的常量版本似乎非常错误。这意味着,虽然您的常量和非常量成员函数恰好具有相同的名称,但它们在fac中我不喜欢静态强制转换的冗长(明确提到类名)。您可以通过使用类型为
const auto*
的局部变量来避免这种情况。为什么静态强制转换适用于此,而不是常量强制转换?@Thebluefish
const强制转换
通常表示丢弃常量(我在这里做相反的操作),我不喜欢使用它进行常规的隐式转换,
const\u cast
对其进行无条件的转换来解释!我会使用
implicit\u cast
而不是
static\u cast
(或者使用@leems建议的本地转换)。即使我必须自己编写,或者从boost导入它…lambda需要C++14或显式返回类型。C++11只能为lambda推导它,其中只包含一个
return
语句。@Angew哦,我不知道,因为g++允许其他语句,我总是这样使用lambda…但很高兴知道,谢谢:)--将宏更新为展开为函数调用。@更改“只有一行返回语句”在每一个主要的编译器中,限制几乎持续了零次。取消了限制的C++14正在进行中,而且就这么简单……我在我的代码库中调用
make_const
作为_const
。并添加一个
T&
T const&
重载。@yakC++17似乎有人说
作为_const()
std:
中的函数:
:因此不必在每个项目不断增长的全局实用程序函数列表中再添加一个函数。这可能不起作用--
call()
可能有很好的理由不保持常量!const call()只有当所创建的对象是一个const,它肯定会调用const fun()时才会被调用。您如何知道
call()
fun()
是同一个类的成员?我们可能会遇到这样一种情况,
a::call()
需要是非常量的,因为它会对其处理的A对象进行变异,但需要使用
B::fun()常量从A对象引用的B中获取某些内容,其中非常量
B::fun()
对B对象做了一些不需要的事情。
struct MyQuestion
{
    void fun()
    {
        std::cout<<"a"; 
    }

    void fun()const
    { 
        std::cout<<"b"; 
    }

    void call()
    {
        void (MyQuestion::*f)()const = &MyQuestion::fun;
        (this->*f)();
    }
};