Allow stream operator on derived classes

2019-08-15 15:20发布

I have an interface that boils down to

class interface
{
    protected:
        virtual void write(std::string const &) = 0;
};

And derived classes like

class derived : public interface
{
    protected:
        void write(std::string const & buf) 
        { 
            std::cout << buf << std::endl; 
        }
};

In my application, these objects are passed around as smart pointers, i.e. std::shared_ptr<derived>. I hoped I could overload the << operator, but only for smart pointer of derivatives of my interface. I tried this:

class interface
{
    /* ... */
    private:
        template <typename Derived> friend typename std::enable_if<
            std::is_base_of<interface, Derived>::value,
            std::shared_ptr<Derived>
        >::type & operator<<(std::shared_ptr<Derived> & lhs,
                std::string const & rhs)
        {
            lhs->write(rhs);
            return lhs;
        }
};

But when I try std::shared_ptr<derived> sp; sp << "test";, the compiler complains that virtual void derived::write(const string&) is protected within this context (this context is my friend function).

Is there a way to achieve this without redundantly writing a stream operator for every derived class?

标签: c++ oop c++11
1条回答
姐就是有狂的资本
2楼-- · 2019-08-15 15:56

Why not simply define your operator as:

friend std::shared_ptr<interface> &operator<<(std::shared_ptr<interface> & lhs, std::string const & rhs);

and pass your objects as std::shared_ptr<interface>?

查看更多
登录 后发表回答