C ++后缀/前缀运算符重载为非成员函数 [英] c++ postfix / prefix operator overload as non-member function

查看:216
本文介绍了C ++后缀/前缀运算符重载为非成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的数组类作为练习.因为,我读到的非成员函数实际上在某些方面比成员函数更好. (斯科特·迈耶斯)

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

如果我使它们成为成员函数并允许* this数组obj以以下格式传递,那么基本上可以解决.

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).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?

我想出的理由是,因为后缀/前缀是一元运算符 他们只有一个参数(通常是* this). 因此,如果我希望编译器隐式提供* this指针并调用重载,则必须将它们实现为成员函数.

The reasoning I came up with is that, since postfix/prefix is unary operator they only have one argument (usually a *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.

我的推理正确吗?如果不是,我如何将其实现为非成员函数? 感谢您为我提供一些见识.

Is My reasoning correct? If not how do I implement this as a non-member function? Thanks for providing me with some insight.

推荐答案

也许我误会了,但是如果您在正确地声明两个运算符时都遇到困难,那么仍然可以使用像成员这样的自由运算符来做到这一点.但是,您确实需要将 object 作为第一个参数按引用传递.您是正确的,作为成员函数,它们可以通过 this 免费获得其对象.作为一项免费功能,您需要自己进行推送.

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. 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;
}

输出

++obj called.
obj++ called.

这篇关于C ++后缀/前缀运算符重载为非成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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