简体   繁体   English

如何在同一个 class 中同时使用成员 function 和朋友 function 运算符重载

[英]How to use both member function and friend function operator overloading in same class

As per an assignment requirement,根据任务要求,

I am trying to implement prefix ++ and postfix ++ operator overloading both as member function and friend function in same class.我正在尝试在同一个 class 中实现前缀 ++ 和后缀 ++ 运算符重载作为成员 function 和朋友 function。

class dist {
private:
    int kMeter;
    int meter;
public:
    // postfix
    dist operator++(int unused) {
        dist dd = *this; 
        (this -> meter)++;
        return dd;
    }

    // prefix
    dist operator++() {
        ++(this -> meter);
        return *this;
    }

    dist(int k = 0, int m = 0) {
        kMeter = k;
        meter = m;
    }

    void print();
    int getMeter() const { return meter; }
    int getKMeter() const { return kMeter; }

    friend dist operator++(dist& d, int unused); //postfix
    friend dist operator++(dist& d); // prefix
};

dist operator++(dist& d, int unused) { // postfix friend func
    dist dd = d; // copy the old value into dd
    d.meter++;
    return dd; // return the original value of d
}

dist operator++(dist& d) { // friend prefix ++
    ++d.meter;
    return d; // return the final value of d
}

But when I trying to use prefix ++ or postfix ++ overloading it shows ambiguous error.但是当我尝试使用前缀 ++ 或后缀 ++ 重载时,它会显示模棱两可的错误。

dist d2(1, 900);
dist d3(3, 800);

/// these following lines showing ambiguous error
cout << d2++ << endl;
cout << ++d3 << endl;

Now,现在,

How can I use both type of operator overloading for same operator in same class file without getting any error.如何在同一个 class 文件中为同一个运算符使用两种类型的运算符重载而不会出现任何错误。

Most operators can be overloaded either as member or nonmember function.大多数运算符可以作为成员或非成员 function 重载。 Some operators ( = [] () -> ) can be overloaded only as members.某些运算符( = [] () -> )只能作为成员重载。 But you cannot have any oeprator both as a member and nonmember, specifically because it is unclear how to pick either one of them.但是您不能同时拥有任何操作员作为成员非成员,特别是因为不清楚如何选择其中任何一个。

What the assignment wants is probably:任务想要的可能是:

  • you overload prefix ++ as a member and the postfix ++ as a nonmember (in your case, also friend)您将前缀 ++ 作为成员重载,将后缀 ++ 作为非成员重载(在您的情况下,也是朋友)
  • create two separate programs: in one, those operators are overloaded as members, in another - as nonmembers创建两个单独的程序:一个,这些运算符作为成员重载,另一个 - 作为非成员

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

相关问题 运算符重载(朋友和成员函数) - operator overloading(friend and member function) 运算符与朋友和成员函数重载 - operator overloading with friend and member function 如何在同一个类中调用另一个运算符重载成员函数的运算符重载成员函数(或使用运算符)? - How can i call a operator overloaded member function(or use the operator) from another operator overloading member function in the same class? 运算符重载为好友函数 - operator overloading as a friend function 实现“-”运算符重载作为类的友元函数 - Realize "-" operator overloading as a friend function of class 操作符重载(使用binaray好友函数)类没有成员,并且成员不可访问 - operator overloading(using binaray friend function) class has no member, and member inaccessible 运算符重载:从成员函数调用朋友函数 - Operator overloading: calling friend function from member function 仅作为成员或朋友工作的嵌套结构的运算符重载 function - Operator overloading for nested struct only working as member or friend function 模板类朋友操作员成员函数 - Templated Class Friend Operator Member Function 如何使用 class 的成员 function 作为另一个 ZA2F2ED4F8EBC2CBB4C21A29 的朋友 function - How to use a member function of a class as friend function of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM