简体   繁体   English

布尔运算符和双运算符重载

[英]Boolean and Double Operator Overloading

Basically I need to overload the operators for boolean and double.基本上我需要重载布尔运算符和双精度运算符。 For boolean I need to overload &&, ||, and << For double I need to overload: + - * / ^ && ||对于布尔值,我需要重载 &&、|| 和 << 对于 double 我需要重载:+ - * / ^ && || << Here is the boolean.h file << 这里是 boolean.h 文件

#ifndef BOOLEAN_H
#define BOOLEAN_H

#include "iostream";

using namespace std;

class Boolean {
public:
    Boolean();
    Boolean(const Boolean& orig);
    virtual ~Boolean();
    //overload the &&, ||, and << operators here.
private:

};

#endif

Here is the double.h file这是 double.h 文件

#ifndef DOUBLE_H
#define DOUBLE_H

class Double {
public:
    Double();
    Double(const Double& orig);
    virtual ~Double();
private:

};

#endif
   virtual ~Boolean();
   friend Boolean operator&&(const Boolean& lhs, const Boolean& rhs)
   {
       // assuming bool truth_ member... adjust as necessary
       return lhs.truth_ && lhs.truth_;
   }
   //similar for ||
   friend std::ostream& operator<<(std::ostream& os, const Boolean& b)
   {
       return os << (b.truth_ ? "True" : "False");
   }

private:

Same kind of thing for the Double operators. Double运算符也是如此。

Alternatively, you could define these outside the class, which is arguably cleaner if they don't need friendship:或者,您可以在课程之外定义这些,如果他们不需要友谊,这可以说是更清洁:

   Boolean operator&&(const Boolean& lhs, const Boolean& rhs)
   {
       // assuming bool is_true() member function... adjust as necessary
       return lhs.is_true() && lhs.is_true();
   }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM