本文介绍了常数& ,&和&& C ++中成员函数的说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在阅读,并遇到了以下几行:

Recently I was reading through the API of boost::optional and came across the lines:

T const& operator *() const& ;
T&       operator *() & ;
T&&      operator *() && ;

我也写了我自己的程序定义成员函数const&和&& (注意,我不是说的返回类型,而是分号之前的分号),他们似乎工作正常。

I also wrote my own program that defines member functions as const&, & and && (Note that I am not speaking about the return type, but the specifiers just before the semi-colons) and they seems to work fine.

我知道声明一个成员函数const的意思,但是任何人都可以解释它声明它的意义const&和&&< / p>

I know what it means to declare a member function const, but can anyone explain what it means to declare it const&, & and &&.

推荐答案

const& 重载将仅用于const,非const和rvalue对象。

const& means, that this overload will be used only for const, non-const and rvalue object.

const A a = A();
*a;

& 只能用于非const对象。

& means, that this overload will be used only for non-const object.

A a;
*a;

&&& 重载将仅用于右值对象。

&& means, that this overload will be used only for rvalue object.

*A();

有关C ++ 11标准的此功能的更多信息,您可以阅读此帖

for more information about this feature of C++11 standard you can read this post What is "rvalue reference for *this"?

这篇关于常数&amp; ,&amp;和&amp;&amp; C ++中成员函数的说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 03:02