重载成员访问运算符 - >,。*(C ++) [英] Overloading member access operators ->, .* (C++)

查看:239
本文介绍了重载成员访问运算符 - >,。*(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解大多数操作符重载,除了成员访问运算符 - > 。* - > * 等。



特别是,传递给这些操作符函数, ?



如何操作符函数(例如 operator->(...)被指向?它能知道吗?它甚至需要知道吗?



最后,是否有任何const注意事项需要考虑?例如,当重载类似 operator [] 的东西时,通常你将需要一个const和非const版本。成员访问操作符是否需要const和非const版本?

解决方案

- >



这是唯一非常棘手的问题。它必须是非静态成员函数,并且不需要参数。返回值用于执行成员查找。



如果返回值是类类型的另一个对象,而不是指针,则随后的成员查找也将由 operator-> 函数。这被称为向下钻取行为。语言链一起 operator-> 调用,直到最后一个返回指针。

  struct client 
{int a; };

struct proxy {
client * target;
client * operator->()const
{return target; }
};

struct proxy2 {
proxy * target;
proxy& operator->()const
{return * target; }
};

void f(){
client x = {3};
proxy y = {& X };
proxy2 z = {& y};

std :: cout<< x.a < y - > a<< z→a。 // print333
}



- > *



这只是棘手的,没有什么特别的。 非重载版本需要一个指向左侧的类类型的对象的对象和一个指向右侧成员类型的指针的对象。但是当你重载它,你可以采取任何你喜欢的参数,并返回任何你想要的。



换句话说,这一个只是一个普通的二进制运算符,如 + - / 。另请参阅:自由运算符> *重载恶意吗?





这些不能重载。当左侧是类类型时,已经有一个内置的含义。也许它有点有意义,能够定义他们为一个指针在左手边,但语言设计委员会决定,将更加混乱比有用。



重载 - > - > * 。* 只能填充表达式未定义的情况,它永远不会改变表达式的含义,即使没有重载也是有效的。 p>

I understand most operator overloading, with the exception of the member access operators ->, .*, ->* etc.

In particular, what is passed to these operator functions, and what should be returned?

How does the operator function (e.g. operator->(...) ) know what member is being refered to? Can it know? Does it even need to know?

Finally, are there any const considerations that need to be taken into account? For example, when overloading something like operator[], generally you will need both a const and non-const version. Do member access operators require const and non-const versions?

解决方案

->

This is the only really tricky one. It must be a nonstatic member function, and it takes no arguments. The return value is used to perform the member lookup.

If the return value is another object of class type, not a pointer, then the subsequent member lookup is also handled by an operator-> function. This is called the "drill-down behavior." The language chains together the operator-> calls until the last one returns a pointer.

struct client
    { int a; };

struct proxy {
    client *target;
    client *operator->() const
        { return target; }
};

struct proxy2 {
    proxy *target;
    proxy &operator->() const
        { return * target; }
};

void f() {
    client x = { 3 };
    proxy y = { & x };
    proxy2 z = { & y };

    std::cout << x.a << y->a << z->a; // print "333"
}

->*

This one is only tricky in that there is nothing special about it. The non-overloaded version requires an object of pointer to class type on the left-hand side and an object of pointer to member type on the right. But when you overload it, you can take any arguments you like and return anything you want. It doesn't even have to be a nonstatic member.

In other words, this one is just a normal binary operator like +, -, and /. See also: Are free operator->* overloads evil?

.* and .

These cannot be overloaded. There is already a built-in meaning when the left-hand side is of class type. Perhaps it would make a little sense to be able to define them for a pointer on the left-hand side, but the language design committee decided that would be more confusing than useful.

Overloading ->, ->*, ., and .* can only fill in cases where an expression would be undefined, it can never change the meaning of an expression that would be valid with no overloading.

这篇关于重载成员访问运算符 - &gt;,。*(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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