当前位置:编程学习 > C/C++ >>

空对象模式---函数需要返回一个无意义的对象或者对象引用的探讨

最近在程序时,遇到一种情况,以前也有遇到过,不过采用另外一种方法绕过了。具体问题如下:

const Layer& Frame::GetLayer(uint32 layerIndex) const
{
  if(CheckLayerIndexValid(layerIndex))
  {
    return m_layers[layerIndex];
  }

  return ?; // what ? how ?
}


在这种情况下,我们可以改为返回指针:

const Layer* Frame::GetLayer(uint32 layerIndex) const
{
  if(CheckLayerIndexValid(layerIndex))
  {
    return m_layers[layerIndex];
  }

  return 0;
}

 

或者作为参数返回:

bool Frame::GetLayer(uint32 layerIndex, Layer &layer) const
{
  if(CheckLayerIndexValid(layerIndex))
  {
    layer = m_layers[layerIndex];
    return true;
  }

  return false;
}

 

但如果我们想保持第一种函数形式,只是想返回对象的引用怎么办?
我想到空对象模式,当需要一个无意义的对象时,可以返回事先确定好的空对象,类似代码如下:

#include <boost/shared_ptr.hpp>

template<class T>
class Nullable
{
protected:
  Nullable(){ }
  virtual ~Nullable(){ }

public:
  static boost::shared_ptr<T>& NullPtr()
  {
    return ms_nullPtr;
  }

  bool IsNull()
  {
    return this == ms_null;
  }

private:
  static T* Null()
  {
    if(0 == ms_null)
    {
      ms_null = new T;
    }

    return ms_null;
  }

private:
  static T *ms_null;
  static boost::shared_ptr<T> ms_nullPtr;
};

template<class T>
T* Nullable<T>::ms_null = 0;
template<class T>
boost::shared_ptr<T> Nullable<T>::ms_nullPtr(Nullable::Null());


class Layer : public Nullable<Layer>
{
public:
  Layer(){ }
  ~Layer(){ }
public:
  int m_id;
};

 

当在上述情况时,我们可以返回 *Layer::NullPtr(),但这样就必须在获得函数返回值后检查是否IsNull()以确定该对象是否有意义,略显麻烦。而且在返回空对象而非引用时,

IsNull()也不再有效。

 

我的例子举得不是太合适,现在这个例子layerIndex确实应该有效,但如果:某个Manager存了一系列以id为键值的layer,需要根据id查找相应的layer,此时查不到也是正常的,此时不应该是错误或异常。

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,