C ++指针分配运算符重载(不仅对象分配,而且指针分配) [英] c++ pointer assignment operator overload(not just object assignment but pointer assignment)

查看:91
本文介绍了C ++指针分配运算符重载(不仅对象分配,而且指针分配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以重载指针分配运算符?
例如当

Is there a way to overload the pointer assignment operator? e.g. overload pointer assignment operator for class A when

A *x, *y;
x = y;


推荐答案

不,这是不可能的。指针类型是标量类型,但是重载运算符需要一个非标量参数。特别是([over.oper] p6):

No, this is not possible. Pointer types are scalar types, but overloading operators requires a non-scalar parameter. In particular ([over.oper]p6):


运算符函数应为非静态成员函数或为非静态成员函数。具有
至少一个参数的成员函数,该参数的类型是类,对类的引用,枚举或对
枚举的引用。

An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

如果您有一个二进制运算符,其中一个参数是用户定义的类型,另一个参数是指针,则可以使用,但是在您要询问的情况下, 操作数是指针。

If you had a binary operator where one parameter was a user-defined type and the other a pointer, that would work, but in the situation you're asking about, both operands are pointers.

这篇关于C ++指针分配运算符重载(不仅对象分配,而且指针分配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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