[英]c++ postfix / prefix operator overload as non-member function
I am writing my own array class as an exercise. 我正在编写自己的数组类作为练习。 Since, I read non-member functions are actually better in some ways than member functions.
因为,我读到的非成员函数实际上在某些方面比成员函数更好。 ( Scott Meyers )
( 斯科特·迈耶斯 )
I am trying to write as many operator overloads as non-member functions as possible. 我正在尝试编写尽可能多的非成员函数运算符重载。 The operator overloads + , - all work out fine as non-member functions.
运算符重载+,-作为非成员函数都可以正常工作。
my_array operator+(const my_array & left, const my_array & right);
my_array operator-(const my_array & operand);
my_array & operator++(); // prefix
my_array operator++(int); //postfix, compiler puts a 0
However, the prefix/postfix operators as non-member functions give issues (they work fine if I use scope resolution and make them member functions) 但是,作为非成员函数的前缀/后缀运算符会产生问题(如果我使用范围解析并将其设为成员函数,它们会很好地工作)
I understand that not every operator overload can be member functions. 我知道并不是每个操作员重载都可以是成员函数。 But , I am having trouble as to why these two cannot be non-member functions.
但是,对于这两个为什么不能成为非成员函数,我有些麻烦。 The error I get is:
我得到的错误是:
: 'my_array& operator++()' must have an argument of class or enumerated type
Which basically can be solved if I make them member functions and allow a *this array obj to be passed along in the following format. 如果我使它们成为成员函数并允许* this数组obj以以下格式传递,则基本上可以解决。
(*this).operator++();
But the whole thing is, I do not want to make them member functions in first place! 但是,总的来说,我不想让它们成为成员函数! So, is it that the pre/post fix operators cannot/should not be implemented as non-member function?
那么,是否/不能将前/后修复运算符实现为非成员函数?
The reasoning I came up with is that, since postfix/prefix is unary operator they only have one argument (usually a *this). 我想出的理由是,由于后缀/前缀是一元运算符,因此它们只有一个参数(通常是* this)。 So, if I want the compiler to provide the *this pointer implicitly and call the overloads, they must be implemented as a member-function.
因此,如果我希望编译器隐式提供* this指针并调用重载,则必须将它们实现为成员函数。
Is My reasoning correct? 我的推理正确吗? If not how do I implement this as a non-member function?
如果不是,如何将其实现为非成员函数? Thanks for providing me with some insight.
感谢您为我提供一些见识。
Perhaps I misunderstood, but if you're struggling with proper declaration of both operators, you can still do this with free operators like members. 也许我误会了,但是如果您正在为两个运算符的正确声明而苦苦挣扎,仍然可以使用像成员这样的自由运算符来做到这一点。 You do, however, need to pass the object as the first parameter by-reference.
但是,您确实需要按引用将对象作为第一个参数传递。 You're correct that as member functions they get their object for free via this .
您是正确的,作为成员函数,他们可以通过this免费获得其对象。 As a free function, you need to push it yourself.
作为一项免费功能,您需要自己推送它。
#include <iostream>
struct my_array
{
// your members here.
};
my_array& operator ++(my_array& obj)
{
// access to members is through obj.member
std::cout << "++obj called." << std::endl;
return obj;
}
my_array operator ++(my_array& obj, int)
{
my_array prev = obj;
// modify obj, but return the previous state.
std::cout << "obj++ called." << std::endl;
return prev;
}
int main(int argc, char *argv[])
{
my_array obj;
++obj;
obj++;
return 0;
}
Output 产量
++obj called.
obj++ called.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.