用于编程练习的重载运算符

Overloading operator for programming exercise

本文关键字:重载 运算符 练习 编程 用于      更新时间:2023-10-16

我在编程课上,需要向我解释重载。简单的问题,所以希望我能很快得到答案。我的理解是,重载运算符允许它在类上使用。如果这是真的,那么我将如何重载>>来处理类?我正在开发一个小程序来测试这个想法,我会在这里发布它

#include <iostream>
#include <cstdlib>
#include "data.h"
using namespace std;
int main() 
{
    data obj;
    cout << "What is the Number?" << endl;
    cin >> obj;
    system("pause");
    return 0;
}
class data
{
    public:
    data operator >> (int);
    private:
};

此页面主要告诉您有关运算符重载的所有信息。

简而言之,对于用户定义类型,C++ 中的几乎每个运算符都可以重载。 某些运算符(如 +、- 或>>)必须在类外部定义,因为它们是独立的,而其他运算符(如复制赋值 (=))必须在类外部定义。

对于您的情况,可以通过以下方式重载>>运算符:

istream& operator>>(istream& in, data& d){
    // Code here
    return in;
}

在显示"此处代码"的位置,将需要读取的代码放入数据对象中。

例如,让我们假设我们正在读取一个具有 x 坐标和 y 坐标的 Point 对象。 它在流中的格式如下:"(x,y)"。 运算符重载可能如下所示:

istream& operator>>(istream& in, Point& p){
    char c;
    in >> c;
    if(c != '('){
        in.clear(ios_base::failbit);
        return in;
    }
    in >> p.x >> c >> p.y >> c;
    return in;
}

这只是一个格式检查最少的示例,但希望它足以让您入门。

请注意,如果类中

的成员是私有的,则应在类定义中与 istream 运算符交朋友:

class data{
    ...
public:
    friend istream& operator>>(istream&, data&);
}

案例1:无需访问私有数据

data.h.
class data {
  public:
    int i;
};
std::ostream& operator>> (std::istream&, data&); // better make operator >>
                                                 // a nonmember function 
                                                 // if it doesn't need access
                                                 // to private data

数据.cpp

#include "data.h"
std::istream& operator>> (std::istream& is, data& d) {
  is>>d.i;    // here we do some logic, we do what it means to do >> on
  return is;  // an instance of your data class and return reference to istream
}

案例2:需要访问私有数据

data.h.
class data {
  private:
    int i;
  friend std::ostream& operator>> (std::istream&, data&);
};

数据.cpp

#include "data.h"
std::istream& operator>> (std::istream& is, data& d) {
  is>>d.i;    // here we do some logic, we do what it means to do >> on
  return is;  // an instance of your data class and return reference to istream
}

如果你想加强对运算符重载的理解,请考虑对象上的所有运算符(如"+"、"++"、"=="、"!="等)基本上都是成员函数。

挑战自己,将Obj a, b; a = b;识别为Obj a; Obj b; a.operator=(b);

重载纯粹是提供非默认实现。

这是 cast-to-const-char* 运算符的 [可怕] 重载:

class BadWolf {
    const char* m_text;
public:
    BadWolf(const char* text) : m_text(text) {}
    // if you really want the original text and ask for it with this function...
    const char* tardisTranslation() const { return m_text; }
    // but if you try to use me as a const char*...
    operator const char* () const { return "bad wolf"; }
};
int main(int argc, const char* argv[]) {
    BadWolf message("hello, sweetie");
    std::cout << "Message reads: " << (const char*)message << std::endl;
    std::cout << "Tardis translation: " << message.tardisTranslaction() << std::endl;
    return 0;
}

我认为这就是你想要的。

class data
{
    friend istream& operator>>( istream&, data& );
    private:
        int data;
};
istream& operator>>( istream& in, data& d )
{
    return in >> d.data;
}