在本文的第一部分,我们向你演示了如何从赋值与一元运算相结合的运算符(例如+=、又如*=等)的默认实现中获益。在这一部分,我们将向你演示如何轻松地创建with_default类以及这个类是如何从赋值运算符的默认实现中获益的。
with_default< type [,defaultValue]>类运用于将有内建类型(如int、long等等)地数据成员在其构造时赋给默认值。注意内建类型的默认构造函数并没有初始化变量。下面是一个例子:
struct Sum { Sum() { /*你并不知道变量‘m_nSumSoFar’的值是多少!!!*/ } private: int m_nSumSoFar; };
在使用with_default类时,你要避免发生上面这样的错误。使用with_default< type [,defaultValue]>涉及到的语法很简单,如下所示
Type是你要给其赋值变量的类型。 DefaultValue是赋值给该变量的值(默认为‘type()’)。 下面是更正后的代码:
struct Sum { Sum() { /* 变量‘m_nSumSoFar’自动设置为0*/ } private: with_default< int> m_nSumSoFar; };
现在,你应该认识到了with_default< type>的重要性,你可能也想如何把它用到你的项目上。为此,你需要运算符+=、-=等等。
下面是为with_default的代码和如何使用它的例子:
#include "operators.h" #include <iosfwd>
// 前面的声明 template< class type, type default_value = type() > struct with_default;
//……与基类一样有用 class no_op {};
template< class type> struct operations_for_type { typedef ops_all< with_default< type>, type> base_class; };
template<> struct operations_for_type< void*> { typedef no_op base_class; };
//……也许有一天VC将允许部分特殊化 #ifndef _MSC_VER template<class ptr> struct operations_for_type< ptr*> { typedef ops_all_for_ptrs< with_default< ptr*> > base_class; }; #endif
template< class type, type default_value = type() > struct with_default : public operations_for_type< type>::base_class { with_default() : m_value( default_value) {} with_default( const type & value) : m_value( value) {}
operator const type () const { return m_value; }
with_default< type, default_value> & operator=( type other) { m_value = other; return *this; }
// 只要这里有类型转化 // 这就要起作用 template< class other_type, other_type other_default_value> with_default< type, default_value> & operator=( const with_default< other_type, other_default_value> & other) { m_value = ( const other_type &)other; return *this; }
private: type m_value; };
template< class char_type, class char_traits, class type, type default_value> std::basic_istream< char_type, char_traits> & operator >>( std::basic_istream< char_type, char_traits> & streamIn, with_default< type, default_value> & value) { type read; streamIn >> read; value = read; return streamIn; }
///////////////////////////////////////////////////// //例子
#include <string> #include <iostream> #include <algorithm>
struct mul_sum_avg { void operator()( int value) { m_multiply *= value; m_sum += value; m_count++; } int mul() const { return m_multiply; } int sum() const { return m_sum; } //……处于举例的缘故,我们假设 // 至少有一个元素 int avg() const { return m_sum / m_count; } private: with_default< int, 1> m_multiply; with_default< int, 0> m_sum; with_default< int, 0> m_count; };
int main(int argc, char* argv[]) { int aVals[] = { 2, 4, 1, 9, 4, 2, 3}; int nCount = sizeof( aVals) / sizeof( aVals[ 0]); mul_sum_avg result = std::for_each( aVals, aVals + nCount, mul_sum_avg()); std::cout << "mul= " << result.mul() << ", sum= " << result.sum() << ", avg=" << result.avg() << std::endl ; return 0; }  
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
|