用于编程练习的重载运算符
Overloading operator for programming exercise
我在编程课上,需要向我解释重载。简单的问题,所以希望我能很快得到答案。我的理解是,重载运算符允许它在类上使用。如果这是真的,那么我将如何重载>>来处理类?我正在开发一个小程序来测试这个想法,我会在这里发布它
#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;
}
相关文章:
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 在 myVector 类中重载运算符 + 时出错
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 如何在 cpp 中重载运算符 +=?
- C++ 如何重载 [] 运算符并进行函数调用
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 重载运算符有地址吗?
- 如何迭代重载运算符 [] 的类?
- 重载运算符与添加问题
- 模板基类中的重载运算符
- 如何调用用于重载运算符"<<"的 friend 函数?
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- 重载运算符<<采用谷歌 C++ 风格
- C++ 如何正确重载 + 运算符
- cout (<<) 重载运算符不打印减去的矩阵
- 如何在 c++ 中重载运算符 + 以便能够 whrite c_str = "smth" + c_str;
- 重载运算符*以获取对另一个类的实例的引用