哪些是C ++中返回值作为常量,参考和常量引用的含义? [英] Which are the implications of return a value as constant, reference and constant reference in C++?

查看:309
本文介绍了哪些是C ++中返回值作为常量,参考和常量引用的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,我仍然对此感到困惑。在C ++中返回值作为常量,引用和常量引用的含义是什么?例如:

  const int exampleOne(); 
int& exampleTwo();
const int& exampleThree();


解决方案

>

•按引用返回:函数调用可用作作业的左侧。例如使用运算符重载,如果你有operator []重载,你可以说

  a [i] = 7; 

(通过引用返回时,您需要确保返回的对象在返回后可用:you不应返回对本地或临时的引用)



•作为常量值返回:阻止在左侧使用该函数的赋值表达式。考虑重载运算符+。可以写如下:

  a + b = c; //这不正确

将operator +的返回类型作为const SomeType



返回常量值也可以防止类似这样的错误:

/ p>

  if(someFunction()= 2)

表示

  if(someFunction()== 2)



如果someFunction()声明为

  const int someFunction()



•作为常量引用返回:此函数调用不能显示在作业的左侧,避免进行复制(通过值返回)。例如。让我们假设我们有一个类Student,我们想提供一个accessor id()来获取学生的ID:

  class Student 
{
std :: string id_;

public:

const std :: string& id()const;
};

const std :: string& Student :: id()
{
return id_;考虑id()存取器。
}



<这应该声明为const,以保证id()成员函数不会修改对象的状态。现在,考虑返回类型。如果返回类型是string&那么可以写如下:

 学生; 
s.id()=newId;

这不是我们想要的。



我们可以通过值返回,但在这种情况下通过引用返回更有效。使返回类型为const字符串&此外还可防止修改ID。


I'm learning C++ and I'm still confused about this. What are the implications of return a value as constant, reference and constant reference in C++ ? For example:

const int exampleOne();
int& exampleTwo();
const int& exampleThree();

解决方案

Here's the lowdown on all your cases:

• Return by reference: The function call can be used as the left hand side of an assignment. e.g. using operator overloading, if you have operator[] overloaded, you can say something like

a[i] = 7;

(when returning by reference you need to ensure that the object you return is available after the return: you should not return a reference to a local or a temporary)

• Return as constant value: Prevents the function from being used on the left side of an assignment expression. Consider the overloaded operator+. One could write something like:

a + b = c; // This isn't right

Having the return type of operator+ as "const SomeType" allows the return by value and at the same time prevents the expression from being used on the left side of an assignment.

Return as constant value also allows one to prevent typos like these:

if (someFunction() = 2)

when you meant

if (someFunction() == 2)

If someFunction() is declared as

const int someFunction()

then the if() typo above would be caught by the compiler.

• Return as constant reference: This function call cannot appear on the left hand side of an assignment, and you want to avoid making a copy (returning by value). E.g. let's say we have a class Student and we'd like to provide an accessor id() to get the ID of the student:

class Student
{
    std::string id_;

public:

    const std::string& id() const;
};

const std::string& Student::id()
{
    return id_;
}

Consider the id() accessor. This should be declared const to guarantee that the id() member function will not modify the state of the object. Now, consider the return type. If the return type were string& then one could write something like:

Student s;
s.id() = "newId";

which isn't what we want.

We could have returned by value, but in this case returning by reference is more efficient. Making the return type a const string& additionally prevents the id from being modified.

这篇关于哪些是C ++中返回值作为常量,参考和常量引用的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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