posts - 13, comments - 4, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Imperfect C++ 读书笔记(五)

Posted on 2008-11-23 18:28 Batiliu 阅读(361) 评论(0)  编辑 收藏 引用 所属分类: 读书笔记

operator bool()

有时候,我们需要用户自定义的类型具有在条件表达式中被自动转换为bool,以满足例如:if (p)、if(!p)、while(std::cin>>obj)等惯用代码。接下来,我们来看看各种各种的解决方案:

  1. operator int () const
    struct X
    {
        operator int() const;
    };
     
    X o;
    std::vector<X> v(o);    // OMG!无声无息(编译器检查不出)走向错误!

     

  2. operator void* () const
    struct X
    {
        operator void*() const;
    };
     
    X o;
    delete o;    // OMG!危险!

     

  3. operator bool () const
    struct X
    {
        operator bool() const;
    };
     
    // 很多人认为让对象从逻辑上表现为一个布尔值的操作符,还有比operator bool()更适合的吗?
    // 很遗憾,请往下看:
     
    X o1;
    X o2;
    int i = o1 + o2;    // bool可以被隐式地转换为int
     
    // 还有更糟糕的:
     
    X o1;
    Y o2;    // Another type
    if (o1 == o2)      // 很可能是语义上错误的代码
    {
        ...
    }

     

  4. operator boolean const * () const
    struct X
    {
    private:
        struct boolean { private: void operator delete(void*); };
    public:
        operator boolean const *() const
        {
            return 0;
        }
    };
     
    // 很好,该方案是Peter Dimvo在Boost新闻组上提出来的。
    // 但基于指针技术的问题在于它们允许向void(const)*转换,这可能是不希望的转换。
     
    X o;
    void const * p = o;

     

  5. operator int boolean::* () const
    class X
    {
    private:
        struct boolean { int i; };
    public:
        operator int boolean::*() const
        {
            return 1 ? &boolean::i : NULL;
        }
    };
     
    // 成员函数的指针不管是通过隐式转换还是显示转换,都不能被转换至常规的指针类型。
    // Perfect!它具备了我们对一个"bool"转换操作符的性质的所有要求

 

operator ++()

首先,我们来看下重载后置增量操作符的规范形式:

struct X
{
    X operator ++(int)
    {
        X ret(*this);
        operator ++();
        return ret;
    }
};

由于后置式操作符在进行增量/减量操作之前必须先把当前值保留下来,这样才能够在增量/减量操作完成后将它返回给调用者,所以它们跟前置式的操作符相比通常具有较高的性能开销。

那我们有什么办法能侦测出代码中对后置操作符的不恰当使用呢?幸运的是,我们在模板的帮助下找到了解决方案:

template<typename V, typename M, typename R = V>
class unused_return_value_monitor
{
public:
    typedef unused_return_value_monitor<V, M, R> class_type;
 
public:
    unused_return_value_monitor(R value, M monitor = M())
        : m_value(value)
        , m_monitor(monitor)
        , m_bUsed(false)
    {}
    unused_return_value_monitor(class_type const& rhs)
        : m_value(rhs.m_value)
        , m_monitor(rhs.m_monitor)
        , m_bUsed(rhs.m_bUsed)
    {
        rhs.m_bUsed = false;
    }
    ~unused_return_value_monitor()
    {
        if (!m_bUsed)
        {
            m_monitor(this, m_value);
        }
    }
public:
    operator V() const
    {
        m_bUsed = true;
        return m_value;
    }
private:
    R m_value;
    M m_monitor;
    mutable bool m_bUsed;
// 声明但不予实现
private:
    unused_return_value_monitor& operator =(class_type const& rhs);
};
 
// 由于这是一个会影响效率的侦测装置,所以我们将它整合进类的调试版本中。
 
class X
{
private:
    struct use_monitor
    {
        void operator ()(void const* instance, X const& value) const
        {
            // 出现“敌情”,输出到调试窗口或日志文件中。
            cout << "Warnning!" << endl;
        }
    };
public:
    X& operator ++()
    {
        // ...
        return *this;
    }
#ifdef _DEBUG
    unused_return_value_monitor<X, use_monitor> operator ++(int)
#else
    X operator ++(int)
#endif
    {
        X ret(*this);
        operator ++();
        return ret;
    }
};

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理