Member access operators
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
访问对象的成员.
Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
---|---|---|---|---|
Inside class definition | Outside class definition | |||
array subscript | a[b]
|
Yes | R& T::operator[](const T2& b); | N/A |
indirection (variable pointed to by a )
|
*a
|
Yes | R& T::operator*(); | R& operator*(T &a); |
address of | &a
|
Yes | R* T::operator&(); | R* operator&(T &a); |
member of object | a.b
|
No | N/A | N/A |
member of pointer | a->b
|
Yes | R* T::operator->() | N/A |
pointer to member of object | a.*b
|
No | N/A | N/A |
pointer to member of pointer | a->*b
|
Yes | R* T::operator->*(R) | R* T::operator->*(T, R) |
|
目录 |
[编辑] 解释
“数组下标”运营商提供访问内部数组中的元素
原文:
array subscript operator provides access to the elements in the internal array
“间接”,“成员的指针”和“指向成员的指针的指针”运营商提供的任何对象的指针的语义.
原文:
indirection, member of pointer and pointer to member of pointer operators provide pointer semantics for any object.
“成员的指针”和“成员的指针的指针”操作符返回一个指针,指向的实际对象,该对象将被用于成员访问.
原文:
member of pointer and pointer to member of pointer operators return a pointer to the actual object which will be used for member access.
[编辑] 内置的下标运算符
对于每一个对象类型
T
(可能被cv修饰),下面的函数签名参与重载决议原文:
For every object type
T
(possibly cv-qualified), the following function signature participates in overload resolution: T& operator[](T*, std::ptrdiff_t); |
||
T& operator[](std::ptrdiff_t, T*); |
||
非指针操作数可以是任何表达式的整数或无作用域的枚举类型,它是隐式转换std::ptrdiff_t。的表达A[B]完全相同表达*(A+B),即,指针操作数(这可能是一个阵列到指针转换结果,以及哪些必须指向一些阵列或过去的端部之一的一个元素)被调整,以点在另一元件相同的数组,后指针运算规则,然后被解除引用.
原文:
The non-pointer operand may be any expression of integral or unscoped enumeration type, it is 隐式转换 to std::ptrdiff_t. The expression A[B] is exactly identical to the expression *(A+B), that is, the pointer operand (which may be a result of array-to-pointer conversion, and which must point to an element of some array or one past the end) is adjusted to point at another element of the same array, following the rules of 指针运算, and is then dereferenced.
#include <iostream> int main() { int a[4] = {1,2,3,4}; int* p = &a[2]; std::cout << p[1] << p[-1] << 1[p] << (-1)[p] << '\n'; }
输出:
4242
[编辑] 内置间接运算符
对于每一个类型
T
,要么是对象类型(可能是合格的简历)或函数类型(不是const或引用合格)的,下面的函数签名参与重载决议原文:
For every type
T
that is either object type (possibly cv-qualified) or function type (not const- or ref-qualified), the following function signature participates in overload resolution: T& operator*(T*); |
||
内置的间接操作符的操作数是一个指针,指向对象或函数,并且其结果是左值,该指针指向。请注意,不完全类型的指针被解除引用,例如:初始化时的参考.
原文:
The operand of the built-in indirection operator is a pointer to object or function, and the result is the lvalue that the pointer is pointing at. Note that a pointer to incomplete type can be dereferenced, e.g. when initializing a reference.
#include <iostream> int f() { return 42; } int main() { int n = 1; int* pn = &n; int& r = *pn; // lvalue can be bound to a reference int m = *pn; // indirection + lvalue-to-rvalue conversion int (*fp)() = &f; int (&fr)() = *fp; // function lvalue can be bound to a reference }
[编辑] 内置的地址操作符
内置的
operator&
的操作数的任何类型的左值表达式或在某些类的非静态成员函数/对象的限定名。这个操作符重载,不参与特殊规则 原文:
The operand of the built-in
operator&
is either an lvalue expression of any type or the qualified name of a non-static member function/object in some class. This operator does not participate in overload resolution, special rules are used: 如果某种类型的操作数是一个左值表达式
T
,operator&
创建并返回一个类型的T*
prvalue,与相同品种的资格,即由操作数指定的对象指向。如果操作数的类型不完全,指针可以形成,但如果发生不完全的类型是一个类,它定义了自己的operator&
,其行为是未定义的。的操作数的类型与用户定义的operator&
std::addressof可用于获得真正的指针.原文:
If the operand is an lvalue expression of some type
T
, operator&
creates and returns a prvalue of type T*
, with the same cv qualification, that is pointing at the object designated by the operand. If the operand has incomplete type, the pointer can be formed, but if that incomplete type happens to be a class that defines its own operator&
, the behavior is undefined. For the operands of type with user-defined operator&
, std::addressof may be used to obtain the true pointer.如果操作数是一个重载的函数的名称,地址,只可以解决,因为如果过载的情况下,也就是“结果”
operator&
是用来初始化一个对象,在铸造表达,在左侧的任务,作为函数参数或在一个return语句.原文:
If the operand is the name of an overloaded function, the address may be taken only if the overload can be resolved due to context, that is, the result of
operator&
is used to initialize an object, in a cast expression, on the left of an assignment, as a function parameter or in a return statement.如果操作数是一个合格的名称的一个非静态成员,例如: &Class::member,结果是prvalue的指针指向成员函数或指针成员对象类型
T
类C
。需要注意的是既不&member也不Class::member甚至也不是(&Class::member)可用于初始化成员指针.原文:
If the operand is a qualified name of a non-static member, e.g. &Class::member, the result is a prvalue pointer to member function or pointer to member object of type
T
in class C
. Note that neither &member nor Class::member nor even (&Class::member) may be used to initialize a pointer to member.void f(int) {} void f(double) {} struct A { int i; }; struct B { void f(); }; int main() { int n = 1; int* pn = &n; // pointer int A::* mp = &A::i; // pointer to member object void (B::*mpf)() = &B::f; // pointer to member function // auto pf2 = &f; // error: ambiguous overloaded function type void (*pf)(int) = &f; // overload resolution due to initialization auto pf2 = static_cast<void(*)(int)>(&f); // overload resolution due to cast }
[编辑] 内置成员访问运算符
内置的
operator.
和operator->
是一个表达的完整类的类型T
(为operator.
)或指针来完成类的类型T*
(operator->
,这是评估前,操作员可以被称为右操作数的名称的左操作数T
或T
的基类的一个成员对象或成员函数,例如expr.member,选购合格的,如expr.name::member,可上通过关键字“模板”,例如expr.template member原文:
The left operand of the built-in
operator.
and operator->
is an expression of complete class type T
(for operator.
) or pointer to complete class type T*
(for operator->
, which is evaluated before the operator can be called. The right operand is the name of a member object or member function of T
or of one of T
's base classes, e.g. expr.member, optionally qualified, e.g. expr.name::member, optionally prepended by the keyword template, e.g. expr.template member.表达A->B是完全等同于(*A).B为内置类型。如果一个用户定义的
operator->
,operator->
再次调用它返回时,递归,直到返回一个普通的指针,operator->
达到的值。之后,内置的语义被应用到该指针.原文:
The expression A->B is exactly equivalent to (*A).B for builtin types. If a user-defined
operator->
is provided, operator->
is called again on the value that it returns, recursively, until the operator->
is reached that returns a plain pointer. After that, builtin semantics are applied to that pointer.在表达expr.B,
1)
如果
B
类型T&,其结果是一个左值T
.原文:
if
B
has type T&, the result is an lvalue T
.2)
如果
B
是一个静态数据成员的类型T
,结果是左值T
指定静态数据成员.原文:
if
B
is a static data member of type T
, the result is an lvalue T
designating that static data member.3)
如果
B
是一个非静态数据成员的类型T
,结果是一个左值,如果expr
是一个左值,其结果是xvalueexpr
值为xValue,其结果是否则一个prvalue。B
宣布是“易变的”成员资格的结果,CV-expr
B
。如果B
是可变的,CV-资格的结果,是工会的挥发性资格.原文:
if
B
is a non-static data member of type T
, the result is an lvalue if expr
is an lvalue, the result is xvalue if expr
is an xvalue, and the result is a prvalue otherwise. If B
is not declared to be a mutable member, the cv-qualification of the result is the union of cv-qualifications of expr
and B
. If B
is mutable, the cv-qualification of the result is the union of volatile-qualifications.4)
的CV-资格的工会如果
B
是一个静态成员函数,其结果是一个左值,指定的静态星期四会员功能。从本质上讲,expr
的评估,并丢弃在这种情况下原文:
if
B
is a static member function, the result is an lvalue designating the static memeber function. Essentially, expr
is evaluated and discarded in this case.5)
B
是一个非静态成员函数,其结果是一种特殊的,只能作为一个函数调用表达式的左操作数的prvalue,不作其他用途原文:
if
B
is a non-static member function, the result is a special kind of prvalue that can only be used as the left-hand operand in a function-call expression, and for no other purpose.6)
B
是一个成员枚举,其结果是是一个prvalue原文:
if
B
is a member enumeration, the result is a prvalue of the.7)
B
嵌套类型,程序是形成不良(不能编译)原文:
if
B
is a nested type, the program is ill-formed (won't compile)8)
如果
expr
类非标量类型和B
的类型名称或decltype符指定的类型相同(减去CV-资格),那么结果是一种特殊的的prvalue,只能作为一个函数调用表达式的左操作数,不作其他用途。函数调用被称为“伪析构函数的调用,它没有参数,返回void,不执行任何操作以外初步评估expr原文:
if
expr
has non-class scalar type and B
is the type name or decltype specifier designating the same type (minus cv-qualifications), then the result is a special kind of prvalue that can only be used as the left-hand operand in a function-call expression, and for no other purpose. The function call is called pseudo destructor call', it takes no arguments, returns void, and performs no action other than initial evaluation of expr.#include <iostream> template<typename T> struct P { typedef T* ptr; }; template<typename T> struct A { class B {}; enum E {RED = 1, BLUE = 2}; int n; static int sn; int f() { return 10+n; } static int fs() { return 4; } A(int n) : n(n) {} // keyword template needed to refer to a dependent template member void g() { T obj; int* p = obj.template ptr<int>; p->~T(); // T is int, this calls int's pseudo destructor } }; template<> int A<P<int>>::sn = 2; int main() { A<P<int>> a(1); std::cout << a.n << ' ' << a.sn << ' ' // << A::sn also works << a.f() << ' ' << a.fs() << ' ' // A::fs() also works << a.RED << ' ' // nested type not allowed // << a.B // nested type not allowed ; }
输出:
1 2 11 4 1
[编辑] 内置的指针成员访问运算符
operator.*
和operator->*
的右操作数的指针类型的成员在课堂上表达T
。operator.*
,左操作数是一个类类型的T
的表达,或一些T
是明确的在访问的基类的派生类。operator->*
,左操作数是一个指向T
或它的基础的 原文:
The right operand of both
operator.*
and operator->*
is an expression of type pointer to member in class T
. For operator.*
, the left operand is an expression of class type T
, or of some derived class in which T
is unambiguous accessible base. For operator->*
, the left operand is a pointer to T
or to its base. 对于每一个组合类型
B
,D
,T
,D
是一样B
或一类派生从B
,并T
是无论是对象或函数的类型,在下面的函数签名参与重载解析:原文:
For every combination of types
B
, D
, T
, where D
is either the same as B
or a class derived from B
, and T
is either object or function type, the following function signature participates in overload resolution: T& operator->*(B*, T D::*); |
||
地方两个操作数可能是被cv修饰,在这种情况下的返回类型的CV-资格是工会的CV资格的操作数
原文:
where both operands may be cv-qualified, in which case the return type's cv-qualification is the union of the cv-qualification of the operands.
的表达E1->*E2是完全等同于(*E1).*E2为内置类型.
原文:
The expression E1->*E2 is exactly equivalent to (*E1).*E2 for built-in types.
为表达expr.*ptr
1)
的动态类型
expr
,如果不包含的成员ptr
,其行为是不确定的原文:
If the dynamic type of
expr
does not contain the member to which ptr
refers, the behavior is undefined2)
CV-资格的规则是相同的成员访问运算符,有一个额外的规则:一个指向成员的指针,指的是一个可变的成员不能使用,修改一个const对象的成员.
原文:
cv-qualification rules are the same as for member access operator, with one additional rule: a pointer to member that refers to a mutable member cannot be used to modify that member in a const object.
4)
expr
是右值及ptr
的成员函数与参考预选赛,该计划是病态的形成原文:
If
expr
is rvalue and ptr
points to a member function with & ref-qualifier, the program is ill-formed5)
如果
expr
是左值和ptr
点&&参考预选赛的成员函数,该计划是病态的形成原文:
If
expr
is lvalue and ptr
points to a member function with && ref-qualifier, the program is ill-formed6)
expr.*ptr
ptr
是一个指向数据成员的结果具有相同的价值范畴expr
原文:
The result of expr.*ptr where
ptr
is a pointer to data member has the same value category as expr
.7)
expr.*ptr
ptr
是一个指向成员函数的指针的结果是一个专责事务可能只被用作一种prvalue左侧参数的函数调用表达式,不作其他用途原文:
The result of expr.*ptr where
ptr
is a pointer to member function is a specail kind of prvalue that may only be used as the left-hand argument of a function call expression and for no other purpose.8)
ptr
是一个空指针值,其行为是不确定的原文:
If
ptr
is a null pointer value, the behavior is undefined#include <iostream> struct S { mutable int mi; int f(int n) { return mi+n; } S(int n) : mi(n) {} }; struct D : public S { D(int n) : S(n) {} }; int main() { int S::* pmi = &S::mi; int (S::*mpf)(int) = &S::f; const S s(7); // s.*pmi = 10; // cannot modify through mutable std::cout << s.*pmi << '\n'; D d(7); // base pointers work with derived object D* dp = &d; std::cout << (d.*mpf)(7) << ' ' << (dp->*mpf)(8) << '\n'; }
输出:
7 14 15
[编辑] 标准库
下标运算符重载的许多标准容器类
原文:
Subscript operator is overloaded by many standard container classes
访问特定位 (公共成员函数of std::bitset )
| |
提供索引访问管理的阵列 原文: provides indexed access to the managed array (公共成员函数of std::unique_ptr )
| |
访问指定位置的字符 (公共成员函数of std::basic_string )
| |
访问指定的元素 (公共成员函数of std::array )
| |
访问指定的元素 (公共成员函数of std::deque )
| |
访问指定的元素 (公共成员函数of std::vector )
| |
访问指定的元素 (公共成员函数of std::map )
| |
访问指定的元素 (公共成员函数of std::unordered_map )
| |
通过索引访问元素 (公共成员函数of std::reverse_iterator )
| |
获得右值引用索引元素 原文: obtains rvalue reference to indexed element (公共成员函数of std::move_iterator )
| |
获取/设置valarray的元素,切片,或面罩 原文: get/set valarray element, slice, or mask (公共成员函数of std::valarray )
| |
返回指定的子匹配 (公共成员函数of std::match_results )
|
间接和成员访问运算符重载,通过的许多迭代器和智能指针类
原文:
The indirection and member access operators are overloaded by many iterators and smart pointer classes
解引用指针到的管理对象 原文: dereferences pointer to the managed object (公共成员函数of std::unique_ptr )
| |
解引用指针到的管理对象 原文: dereferences pointer to the managed object (公共成员函数of std::shared_ptr )
| |
访问管理的对象 (公共成员函数of std::auto_ptr )
| |
一个参考这个raw_storage_iterator的返回 原文: returns a reference to this raw_storage_iterator (公共成员函数of std::raw_storage_iterator )
| |
解引用递减的底层迭代器 原文: dereferences the decremented underlying iterator (公共成员函数of std::reverse_iterator )
| |
no-op (公共成员函数of std::back_insert_iterator )
| |
no-op (公共成员函数of std::front_insert_iterator )
| |
no-op (公共成员函数of std::insert_iterator )
| |
访问指向的元素 (公共成员函数of std::move_iterator )
| |
获得的电流的副本element accesses当前元素的成员 原文: obtains a copy of the current element accesses a member of the current element (公共成员函数of std::istream_iterator )
| |
no-op (公共成员函数of std::ostream_iterator )
| |
(C++11 起) |
获得的电流的副本character accesses的当前字符的成员,如果 CharT 有成员 原文: obtains a copy of the current character accesses a member of the current character, if CharT has members (公共成员函数of std::istreambuf_iterator )
|
no-op (公共成员函数of std::ostreambuf_iterator )
| |
访问当前match accesses当前匹配的成员 原文: accesses the current match accesses a member of the current match (公共成员函数of std::regex_iterator )
| |
访问当前result accesses的成员的当前结果 原文: accesses the current result accesses a member of the current result (公共成员函数of std::regex_token_iterator )
|
{{{1}}}
[编辑] 另请参阅
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||