在 const 和 volatile 上重载 - 为什么它通过引用起作用? [英] Overloading on const and volatile- why does it work by reference?

查看:53
本文介绍了在 const 和 volatile 上重载 - 为什么它通过引用起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

#include "stdafx.h"
#include <iostream>

using namespace std;


void func(const int& a)
{
    std::cout << "func(const)" << std::endl;
}

void func(volatile int& a)
{
    std::cout << "func(volatile)" << std::endl;
}

void func(const volatile int& a)
{
    std::cout << "func(const volatile)" << std::endl;
}

int main()
{
    const int a = 0;
    const volatile int b = 0;
    volatile int c = 0;
    func(a);
    func(b);
    func(c);
    system("pause");
    return 0;
}

上面的代码显示了基于参数是否为 const/volatile 的重载.但是,如果我将参数从 int& 更改为 int,代码将不再编译,并且我无法根据 const/volatile 参数类型重载.我不明白为什么如果 int 通过引用传递,我们可以基于 const 和 volatile 重载,但如果它通过值传递则不能?

The above code shows overloading based on whether the parameters are const/volatile. However, if I were to change the parameters from int& to int, the code no longer compiles and I cannot overload based upon const/volatile parameter types. I dont get why we can overload based on const and volatile if the int is passed by reference, but not if its passed by value?

编辑我应该强调我理解引用的作用——我不明白为什么允许引用别名在 const 上重载,而普通的 int 不允许.

EDIT I should emphasise I understand what a reference does- I do not understand why a reference alias is allowed to overload on const but a normal int is not.

推荐答案

也许从函数中退一步,只看用例本身是有用的.

Perhaps it is useful to take a step back from the functions and just look at the use-cases themselves.

首先,我们将定义一个整数和一个常量整数以供我们的示例使用:

First, we will define an integer and a constant integer for use in our examples:

int       anInt     = 1;
const int aConstInt = 1;

接下来,我们看看使用这些变量设置其他整数和常量整数的值时会发生什么:

Next, we take a look at what happens when using these variables to set the values of other integers and constant integers:

int       a = anInt;     // This works, we can set an int's value
                         //  using an int
int       b = aConstInt; // This works, we can set an int's value
                         //  using a const int
const int c = anInt;     // This works, we can set a const int's value
                         //  using an int
const int d = aConstInt; // This works, we can set a const int's value
                         //  using a const int

正如您所见,没有办法根据行为来解决要选择的函数的哪个重载(一个 const int 可以被 int 和 const int 接受,同样一个 int 可以被两个int 和一个 const int).

As you can see, there is no way to resolve which overload of a function to select based on behavior (a const int can be accepted by both an int and a const int, and likewise an int can be accepted by both an int and a const int).

接下来,我们来看看将第一组变量传递给引用时会发生什么:

Next, we shall take a look at what happens when pass the first set of variables to references:

int& a = anInt;     // This works because we are using a
                    //  non-constant reference to access a
                    //  non-constant variable.
int& b = aConstInt; // This will NOT work because we are
                    //  trying to access a constant
                    //  variable through a non-constant
                    //  reference (i.e. we could
                    //  potentially change a constant
                    //  variable through the non-const
                    //  reference).

const int& c = anInt;     // This works because we are using a
                          //  constant reference (i.e. "I cannot
                          //  try to change the referenced
                          //  variable using this reference") to
                          //  a non-constant variable.
const int& d = aConstInt; // This will work because we are trying
                          //  to access a constant variable 
                          //  through a constant reference.

如您所见,在区分 int 引用和 const int 引用时可能会产生一些有用的行为(即,当需要常量引用类型时,不允许创建非常量引用).

As you can see, there is some useful behavior that can be had out of distinguishing between an int reference and a const int reference (i.e. disallowing creation of a non-constant reference when a constant reference type is expected).

这篇关于在 const 和 volatile 上重载 - 为什么它通过引用起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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