effective c++:尽量替换define,确保对象使用前初始化

#define ASPECT_RATIO 1.653

名为ASPECT_RATIO的值在预编译阶段被替换成1.653,如果在这个常量上出现编译错误,我们可能会困惑1.653的值是什么意思,于是将因为跟踪它而浪费时间。

我们需要使用一个常量来替换上述的宏

const double AspectRatio = 1.653;

当我们以常量替换#define 时,有两种特殊情况需要考虑

第一:常量指针的定义放在头文件内,需要被不同的源代码访问,因此有必要将指针声明为const,如:

const char* const authorName = "Scott Meyers"

或者写成c++形式:

const std::string authorName("Scott Meyers")

第二:为了将常量的作用域限制于class 中,必须让他成为class的一个成员,而为了确保此常量至多只用一份实体,需要声明为static:

class GamePlayer{
 private:
  static const int NumTurns = 5;
  int scores[NumTurns];            
};

另一个常见#define误用的情况是以它实现宏max(a,b) f((a)>(b)? (a):(b)),该宏看起来像个函数,而且不会招致函数带来的额外开销。

但这往往会带来意想不到的结果:

int a = 5 ,b = 0;
max(++a,b);
max(++a,b+10);

第一次调用max时a累加两次,而第二次调用max 时a累加一次,我想这应该不是编写这段程序的人的本意。

遇到上述问题,我们需要使用inline 来替换:

template <class T>
inline void max(const T& a,const T& b)
{
  f(a>b ? a:b);  
}

这样一来就不用操心参数被求值几次的问题了。

对象使用前初始化

对于无成员内置类型,必须手工完成初始化:

int x = 0;
const char* text = "a c-style string";
double d;
std::cin >> d;

至于内置类型外的初始化需要在构造函数中完成,确保每个构造函数都将对象的每个成员初始化。

class PhoneNumber { ... };
class ABEntry { // ABEntry = “Address Book Entry”
public:
  ABEntry(const std::string& name, const std::string& address,
  const std::list<PhoneNumber>& phones);
private:
  std::string theName;
  std::string theAddress;
  std::list<PhoneNumber> thePhones;
  int numTimesConsulted;
};
ABEntry::ABEntry(const std::string& name, const std::string& address,
const std::list<PhoneNumber>& phones)
{
  theName = name; // these are all assignments,
  theAddress = address; // not initializations
  thePhones = phones;
  numTimesConsulted = 0;
}

 上面这段代码在构造函数体内对成员变量进行赋值,注意:这不叫初始化,真正的初始化是这样的:

ABEntry::ABEntry(const std::string& name, const std::string& address,
const std::list<PhoneNumber>& phones)
: theName(name),
theAddress(address), // these are now all initializations
thePhones(phones),
numTimesConsulted(0)
{}

 

 这两个版本的构造函数在运行时效果一样,但后者往往效率更高,基于赋值的构造函数在调用构造函数体前已经为成员变量设了初始值,函数体内又进行了一次赋值,从而造成程序运行成本增加。另一方面如果成员变量是const或references就不能赋值,必须在初始化时确定。

初始化的顺序最好按声明顺序来,避免出现不必要的错误,例如:初始化一个数组前,首先初始化数组长度。

对于初始化次序不确定的情况(对不同编译单元的non-local static),则需要以local static替换之。所谓的local static对象指的是函数内的static 对象,其他则成为non-local static 对象。

假设有一个文件系统:

class FileSystem { // from your library’s header file
public:
...
std::size_t numDisks() const; // one of many member functions
...
};
extern FileSystem tfs; // declare object for clients to use
// (“tfs” = “the file system” ); definition
// is in some .cpp file in your library

其中tfs预留给客户使用,如果在客户使用tfs 之前,tfs还没有初始化,就会造成严重后果。

class Directory { // created by library client
public:
Directory( params );
...
};
Directory::Directory( params )
{
...
std::size_t disks = tfs.numDisks(); // use the tfs object
...
}

Directory tempDir( params ); // 创建一个
Directory对象

 其中的tfs与tmpDir是不同人不同时间建立起来的,定义于不同编译单元内,这种情况下就无法保证tfs在tempDir之前被初始化。

我们需要做的就是把non-local static对象放到函数内变为local static,然后让用户调用该函数,这样就可以确保初始化的正确顺序。

class FileSystem { ... }; // as before
FileSystem& tfs() // this replaces the tfs object; it could be
{ // static in the FileSystem class
  static FileSystem fs; // define and initialize a local static object
  return fs; // return a reference to it
}
class Directory { ... }; // as before
Directory::Directory( params ) // as before, except references to tfs are
{ // now to tfs()
  ...
  std::size_t disks = tfs().numDisks();
  ...
}
Directory& tempDir() // this replaces the tempDir object; it
{ // could be static in the Directory class
  static Directory td( params ); // define/initialize local static object
  return td; // return reference to it
}

 

 

原文地址:https://www.cnblogs.com/loujiayu/p/3588614.html