我正在尝试创建一个计数器接口,强制所有派生类实现此接口:
class CounterInterface
{
public:
virtual CounterInterface& operator ++ () = 0;
virtual CounterInterface operator ++ (int) = 0;
virtual CounterInterface& operator -- () = 0;
virtual CounterInterface operator -- (int) = 0;
virtual bool operator == ( const CounterInterface o ) const = 0;
virtual operator uint32_t () const = 0;
virtual void reset() = 0;
};
但是,仅包含此类定义会导致以下错误。
不幸的是 post inc 不能被定义为参考。
任何想法如何解决这个鸡/蛋问题?
CounterInterface.h:25:29: error: invalid abstract return type for member function ‘virtual libceis::CounterInterface libceis::CounterInterface::operator++()’
CounterInterface.h:22:8: note: because the following virtual functions are pure within ‘libceis::CounterInterface’:
CounterInterface.h:25:29: note: virtual libceis::CounterInterface libceis::CounterInterface::operator++()
CounterInterface.h:26:29: note: virtual libceis::CounterInterface libceis::CounterInterface::operator++(int)
CounterInterface.h:27:29: note: virtual libceis::CounterInterface libceis::CounterInterface::operator--()
CounterInterface.h:28:29: note: virtual libceis::CounterInterface libceis::CounterInterface::operator--(int)
CounterInterface.h:29:17: note: virtual bool libceis::CounterInterface::operator==(libceis::CounterInterface) const
CounterInterface.h:30:12: note: virtual libceis::CounterInterface::operator uint32_t() const
CounterInterface.h:31:17: note: virtual void libceis::CounterInterface::reset()
CounterInterface.h:26:29: error: invalid abstract return type for member function ‘virtual libceis::CounterInterface libceis::CounterInterface::operator++(int)’