ACE源代码之array.cpp

来源:互联网 发布:知乎北京酒店 编辑:程序博客网 时间:2024/04/30 19:50

      作为研究的开始,本文首先从ACE代码中最简单的数组开始研读,如庖丁解牛。让这个庞然大物的五脏六腑,奇经八脉一点点清晰的展现在你眼前。本文研究的过程中结合《Effective c++》中的c++编程技巧,体会大家的风范。

// Dynamically initialize an array.

template <class T>
ACE_Array<T>::ACE_Array (size_t size)
  : max_size_ (size),
    cur_size_ (size)
{
  ACE_NEW (this->array_, T[size]);
}

template <class T>
ACE_Array<T>::ACE_Array (size_t size,
                 const T &default_value)
  : max_size_ (size),
    cur_size_ (size)
{
  ACE_NEW (this->array_, T[size]);

  for (size_t i = 0; i < size; i++)
    this->array_[i] = default_value;
}

// The copy constructor (performs initialization).

template <class T>
ACE_Array<T>::ACE_Array (const ACE_Array<T> &s)
   : max_size_ (s.size ()),
     cur_size_ (s.size ())
{
  ACE_NEW (this->array_, T[s.size ()]);

  for (size_t i = 0; i < this->size (); i++)
    this->array_[i] = s.array_[i];
}

 

 

// Assignment operator (performs assignment).

template <class T> void
ACE_Array<T>::operator= (const ACE_Array<T> &s)
{
  // Check for "self-assignment".

  if (this == &s)
    return;


//如果赋值的过程中,被赋值对象的存储空间小于赋值对象的空间,则需要首先将原对象删除,并开辟一块新的存储空间。


  else if (this->max_size_ < s.size ())
    {
      delete [] this->array_;

      ACE_NEW (this->array_, T[s.size ()]);

      this->max_size_ = s.size ();
    }

  this->cur_size_ = s.size ();

  for (size_t i = 0; i < this->size (); i++)
    this->array_[i] = s.array_[i];
}

// Set an item in the array at location index.

template <class T> int
ACE_Array<T>::set (const T &new_item, size_t index)
{

//在为数组中指定元素赋值的过程中,索引的范围检测是必不可少的。

 

//在ACE中作者采用一个专门的方法来检测索引,这样可以避免在不同的地方重复的编写同样的测试代码

//将“责任”集中有利于以后的修改和维护,这种思想是很好的

 

  if (this->in_range (index))
    {
      this->array_[index] = new_item;
      return 0;
    }
  else
    return -1;
}

 

 

// Get an item in the array at location index.

template <class T> int
ACE_Array<T>::get (T &item, size_t index) const
{
   if (this->in_range (index))
     {
       item = this->array_[index];  // Copies the item.  If you don't
                                    // want to copy, use operator [] instead
                                    // (but then you'll be responsible for
                                    // range checking).
       return 0;
     }
   else
     return -1;
}

// Compare this array with <s> for equality.

template <class T> int
ACE_Array<T>::operator== (const ACE_Array<T> &s) const
{

//好的程序中比较重要的一点是,让方法(函数)尽早的返回。

//下面的代码中首先测试了是否是本身,以及大小是否相等等特殊情况

//如果这些情况出现,程序则返回,而不用运行很多逻辑,对于提高效能很有必要。
  if (this == &s)
    return 1;
  else if (this->cur_size_ != s.cur_size_)
    return 0;

 

 

  for (size_t index = 0; index < s.cur_size_; index++)
    if (this->array_[index] != s.array_[index])
      return 0;

  return 1;
}

 

//下面的这个方法是获取下一个元素的内容,这在Java中式经常出现的,尤其是容器的遍历过程中

//本方法的返回值用于判断下一个位置的合法性

//个人认为返回bool值更具有可读性

 

template <class T> int
ACE_Array_Iterator<T>::next (T *&item)
{
  // ACE_TRACE ("ACE_Array_Iterator<T>::next");

 

  if (this->done ())
    {
      item = 0;
      return 0;
    }
  else
    {
      item = &array_[current_];
      return 1;
    }
}

#endif /* ARRAY_C */

 

本篇讲解:对象引用的优点

对象引用于普通的参数传递最大的不同在于是否有对象拷贝的过程。在需要向函数传递大对象时,需要使用引用形参。虽然赋值实参对于内置数据类型的对象或者规模较小的类类型来说没有什么问题,但是对于大部分的类类型或者大型数组,它的效率就比较低了。某些类类型是无法复制的。使用引用形参,函数可以直接访问实参对象,无须复制。

     版权所有。本作者保留本文相关权利,如需转载请注明出处。

原创粉丝点击