2017-06-19 17 views
6

想象一下如下設置。如何從派生類cout中調用基類cout?我可以使用getBrand()方法,但我覺得我應該能夠直接訪問基類'cout朋友函數。如何從派生類中調用重載的父cout朋友類?

我砍了一下,試了this.Brand,也只是Brand。沒有運氣。

class Brand { 
public: 
    Brand(std::string brand):brand_(brand) {}; 
    friend std::ostream & operator << (std::ostream & out, const Brand & b) { 
     out << b.brand_ << ' '; 
     return out; 
    } 
    std::string getBrand()const { return brand_; }  
private: 
    std::string brand_; 
} 

class Cheese : public Brand { 
public: 
    Cheese(std::string brand, std::string type):Brand(brand), type_(type) {}; 
    friend std::ostream & operator << (std::ostream & out, const Cheese & c) { 
     out << /* THIS.BRAND?! BRAND?! getBrand() meh.. */ << ' ' << c.type_ << std::endl; // <-- HERE 
     return out; 
    } 
private: 
    std::string type_; 
} 

int main() { 
    Cheese c("Cabot Clothbound", "Cheddar"); 
    std::cout << c << std::endl; 
} 

所需的輸出

Cabot Clothbound Cheddar 
+0

當Brand的'operator <<'將尾部換行放入流中時,輸出應該如何成爲'Cabot Clothbound Cheddar'? – Zereges

+0

好點,但在旁邊。編輯並刪除。 – kmiklas

回答

6

你可以從你的派生類中調用基類的重載operator <<。因爲,你聲明的運營商作爲一個朋友,你可以簡單地把派生類的基類:

class Cheese : public Brand { 
public: 
    Cheese(std::string brand, std::string type):Brand(brand), type_(type) {}; 
    friend std::ostream & operator << (std::ostream & out, const Cheese & c) { 

     //ADDED 
     out << static_cast<const Brand&>(c) << c.type_ << std::endl; 
     return out; 
    } 
private: 
    std::string type_; 
}; 

輸出:

Cabot Clothbound Cheddar 

See it Live

+0

比你快1分鐘,但你有一個現場演示,而我沒有一個,所以採取我的upvote。 =) – gsamaras

2

演員,就像這樣:

friend std::ostream& operator<<(std::ostream& out, const Cheese& c) 
{ 
    out << static_cast<const Brand &>(c); 
    out << c.type_ << std::endl; 

    return out; 
} 
1

顯然,你不能這樣做類似於Brand::operator<<,因爲operator<<定義爲friend,因此它們不是成員函數。

如果你想調用operator<<(std::ostream&, const Brand&),你只需要通過正確的類型這一點,因爲派生類可以很容易地鑄造到基類,你可以做

friend std::ostream & operator << (std::ostream & out, const Cheese & c) { 
    out << static_cast<const Brand&>(c) << ' ' << c.type_ << std::endl; 
    return out; 
} 
2

其他所有的答案都正確響應你的具體問題,但每當你嘗試使用多態是這樣的:

Brand const &c = Cheese("Cabot Clothbound", "Cheddar"); 
std::cout << c << std::endl; 

operator <<對應Brand將被調用,而不是Cheese的。

好辦法做到這一點是使用虛擬打印成員函數:

class Brand { 
public: 
    Brand(std::string const & brand):brand_(brand) {} 
    virtual ~Brand() {} 
    virtual void print(std::ostream & out) const { 
     out << brand_; 
    } 
    std::string const & getBrand()const { return brand_; }  
private: 
    std::string brand_; 
}; 
class Cheese : public Brand { 
public: 
    Cheese(std::string const & brand, std::string const & type):Brand(brand), type_(type) {} 
    void print(std::ostream & out) const override { 
     Brand::print(out); // calling base print() 
     out << ' ' << type_ << std::endl; 
    } 
private: 
    std::string type_; 
}; 

然後,你只需要在基類,它會叫你printoperator <<虛擬會員功能:

std::ostream & operator << (std::ostream & out, const Brand & b) { 
    b.print(out); 
    return out; 
} 

DEMO

+0

優秀的迴應;這次真是萬分感謝。儘管它沒有像上述那樣直接回答問題,但我非常想要接受這個答案。無論如何,感謝您花時間教我這個,我很感激。 :^) – kmiklas