实现全功能的移位流语法

时间:2016-05-17 01:49:31

标签: c++ operator-overloading

在我班上,我重载了运算符<<为了实现

MyObject << something1 << something2;

friend const std::string& operator<<(Logger& os, const std::string& msg);

但我可以做到:

MyObject&lt;&lt; something1;

如果我添加第二个流输入(something2),我收到编译错误,告诉我没有输入操作符。

那么,我的问题是:我如何实现预期的语法?

2 个答案:

答案 0 :(得分:2)

os << v1 << v2必须返回流(作为参考)。换句话说,它应该返回 first (左手)参数。

(os << v1) << v2被解析为os << v1,因此对于外部调用使用相同的重载运算符,os的结果最好与{{1}}的类型相同

答案 1 :(得分:0)

好的,我发现了问题。我需要返回MyObject作为参考:

SELECT
    dc.dateasdate AS 'Date',
    CASE
        WHEN dc.dateasdate IS NULL THEN '0'
        ELSE COUNT(Practice)
    END AS 'Total Enquiries',
    dp.practice,
    de.Origin
FROM dim.Practice dp
LEFT JOIN fact.enquiry fe
    ON fe.PracticeSKey = dp.PracticeSKey
LEFT JOIN dim.EnquiryOrigin de
    ON de.EnquiryOriginSKey = fe.EnquiryOriginSKey
    AND de.Origin = 'Web Enquiry'
LEFT JOIN dim.Calendar dc
    ON dc.CalendarSKey = fe.EnquiryCreatedSKey
    AND dc.dateasdate = '2016-04-03'
WHERE
    de.Origin IS NOT NULL
GROUP BY
    dc.dateasdate,
    de.Origin,
    practice
ORDER BY dc.dateasdate, Practice