简体   繁体   English

。*,-> *和.->在C ++中是什么意思?

[英]What does .*, ->* and .-> mean in C++?

I have been recently learning how to use the std::mem_fn library facility. 我最近一直在学习如何使用std::mem_fn库工具。 In C++ Primer 5th edition they give an example of a usage of std::mem_fn , in the following code snippet svec is a vector of string s: 在C ++ Primer 5th Edition中,他们给出了std::mem_fn用法的示例,在以下代码段中svecstring s的vector

auto f = mem_fn(&string::empty); // f takes a string or a string*

f(*svec.begin()); // ok: passes a string object; f uses .* to call empty

f(&svec[0]); // ok: passes a pointer to string; f uses .-> to call empty

Note: In the above code snippet there is no use of ->* 注意:在上面的代码片段中,没有使用-> *

Although I understand the usage .* and ->* . 虽然我了解.*->*的用法。 I don't have clarity on it. 我不清楚。

So, My question is what does .* , ->* , .-> do? 所以,我的问题是.*->*.->做什么的?

You have an object o and a pointer to its member m . 您有一个对象o和一个指向其成员m的指针。 The operator .* allows you to access the pointed member of your object: 运算符.*允许您访问对象的指定成员:

o.*m

You have a pointer to an object p and a pointer to its member m . 您有一个指向对象p的指针和一个指向其成员m的指针。 The operator ->* allows you to access the pointed member of the pointed object: 运算符->*允许您访问指向对象的指向成员:

p->*m

I am afraid that the operator .-> doesn't exist. 恐怕运算符.->不存在。

The .* and ->* operators are called pointer to member access operators and like their name suggests they allow you to access an object's data or function member given an appropriate pointer to that class member. .*->*运算符称为指向成员访问运算符的指针,就像它们的名称一样,它们建议您在给定指向该类成员的适当指针的情况下,允许您访问对象的数据或函数成员。

struct foo
{
    int a;
    float b;
    void bar() { std::cout << "bar\n"; }
};

int main()
{
    int foo::* ptra = &foo::a;
    float foo::* ptrb = &foo::b;
    void (foo::* ptrbar)() = &foo::bar;

    foo f;
    foo * fptr = &f;

    f.*ptra = 5;
    f.*ptrb = 10.0f;
    (f.*ptrbar)();

    fptr->*ptra = 6;
    fptr->*ptrb = 11.0f;
    (fptr->*ptrbar)();

    return 0;
}

Operators .* and ->* call a member function (BTW there is no meaning for .-> in C++ - it's a syntax error). 运算符.*->*调用成员函数(顺便说一句,C ++中.->没有意义-这是语法错误)。

These are binary operators. 这些是二进制运算符。 The left operand is an object (for .* ) or a pointer to an object (for ->* ), and the right operand is a pointer to a member (which can be a member field or a member function). 左边的操作数是一个对象(用于.* )或指向该对象的指针(对于->* ),而右边的操作数是指向成员(可以是成员字段或成员函数)的指针。 The outcome of applying this operator is the ability to use a member field or a method. 应用此运算符的结果是使用成员字段或方法的能力。

Maybe these operators make sense from a historical point of view. 从历史的角度来看,这些运营商也许是有意义的。 Suppose you use a regular C pointer to a function: 假设您使用一个指向函数的常规C指针:

typedef int (*CalcResult)(int x, int y, int z);
CalcResult my_calc_func = &CalcResult42;
...
int result = my_calc_func(11, 22, 33);

What if you decided to "rewrite" your program in an object-oriented way? 如果您决定以面向对象的方式“重写”程序怎么办? Your calculation function would be a method in a class. 您的计算函数将是类中的方法。 To make a pointer to it, you need a new type, a pointer to member function: 要指向它,需要一个新类型,一个指向成员函数的指针:

typedef int (MyClass::*CalcResult)(int x, int y, int z);
CalcResult my_calc_func = &MyClass::CalcResult42;
...
MyClass my_object;
int result = (my_object.*my_calc_func)(11, 22, 33);

I always use this analogy when I try to remember the syntax of this .* operator - this helps because the regular pointer-to-function syntax is less convoluted and used more frequently. 当我尝试记住此.*运算符的语法时,我总是使用这种类比-这很有用,因为常规的指针到函数的语法不那么复杂,而且使用频率更高。

Please note the parentheses: 请注意括号:

int result = (my_object.*my_calc_func)(11, 22, 33);

They are almost always needed when using this .* operator, because its precedence is low. 使用此.*运算符时,几乎总是需要它们,因为其优先级较低。 Even though my_object .* (my_calc_func(11, 22, 33)) is meaningless, the compiler would try to make sense of it if there were no parentheses. 即使my_object .* (my_calc_func(11, 22, 33))是没有意义的,但是如果没有括号,编译器也会尝试使其有意义。

The ->* operator works in essentially the same way. ->*运算符的工作方式基本上相同。 It would be used if the object on which to apply the method is given by a pointer: 如果要在其上应用该方法的对象是由指针指定的,则将使用它:

typedef int (*MyClass::CalcResult)(int x, int y, int z);
CalcResult my_calc_func = &MyClass::CalcResult42;
...
MyClass my_object1, my_object2;
MyClass* my_object = flag ? &my_object1 : &my_object2;
int result = (my_object->*my_calc_func)(11, 22, 33);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM