2014-04-14 77 views
-1

基本上我需要重載布爾運算符和雙重運算符。 對於布爾我需要重載& &,||和< < 對於雙我需要重載: + - */^ & & || < < 這裏是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 

這裏是double.h文件

#ifndef DOUBLE_H 
#define DOUBLE_H 

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

}; 

#endif 
+0

它是什麼,你希望人們對你到底做什麼?爲什麼你重新實現基本類型......? –

+0

預處理器指令不應以分號結尾。無論如何,[運算符重載](http://stackoverflow.com/questions/4421706/operator-overloading)。 – chris

+2

警告:重載的'&&'和'||'運算符不會產生程序員習慣的「短路」行爲。 – aschepler

回答

0
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: 

這樣的事情對於Double運營商。

或者,你可以在類,這可以說是清潔之外定義這些,如果他們不需要友誼:

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