我们是否需要在 class 的构造函数开头清除 vector 类型的 class 成员?

Do we need to clear a class member of type vector in beginning of the constructor function of the class?

我试图理解此 pdf 中 class 的 class 定义:http://finance.bi.no/~bernt/gcc_prog/recipes/recipes.pdf(第 61-62 页)。我有问题的代码部分如下。以下是头文件:

#ifndef _TERM_STRUCTURE_CLASS_INTERPOLATED_
#define _TERM_STRUCTURE_CLASS_INTERPOLATED_

#include "term_structure_class.h"
#include <vector>
using namespace std;

class term_structure_class_interpolated : public term_structure_class {
private:

    vector<double> times_; // use to keep a list of yields
    vector<double> yields_;
    void clear();

public:

    term_structure_class_interpolated();
    term_structure_class_interpolated(const vector<double>& times, const vector<double>& yields);
    virtual ˜term_structure_class_interpolated();
    term_structure_class_interpolated(const term_structure_class_interpolated&);
    term_structure_class_interpolated operator= (const term_structure_class_interpolated&);

    int no_observations() const { return times_.size(); };
    virtual double r(const double& T) const;
    void set_interpolated_observations(vector<double>& times, vector<double>& yields);

};
#endif

下面是定义 class 方法的代码文件:


#include "fin_recipes.h"

void term_structure_class_interpolated::clear(){

    times_.erase(times_.begin(), times_.end());
    yields_.erase(yields_.begin(), yields_.end());

};

term_structure_class_interpolated::term_structure_class_interpolated():term_structure_class(){clear();};

term_structure_class_interpolated::term_structure_class_interpolated(const vector<double>& in_times,
const vector<double>& in_yields) {

    clear();
    if (in_times.size()!=in_yields.size()) return;
    times_ = vector<double>(in_times.size());
    yields_ = vector<double>(in_yields.size());
    for (int i=0;i<in_times.size();i++) {
        times_[i]=in_times[i];
        yields_[i]=in_yields[i];
    };

};

term_structure_class_interpolated::˜term_structure_class_interpolated(){ clear();};

\ rest of the code not included here

如果你看一下构造函数和析构函数的两个定义,一开始就调用了方法clear(),据我了解,这使得class成员的大小向量零。我不明白为什么要这样做?它们的大小不是已经为零了吗?

我的第二个问题是为什么父 class(术语结构 class)在下面的行中被引用:

term_structure_class_interpolated::term_structure_class_interpolated():term_structure_class(){clear();};

第三个问题是:我们不能简单地在另一个构造函数定义中使用赋值运算符来初始化时间和产量,而不是像这样使用 for 循环逐个元素地复制吗?

term_structure_class_interpolated::term_structure_class_interpolated(const vector<double>& in_times,
const vector<double>& in_yields) {

    times_ = in_times;
    yields_ = in_yields;

};
  1. 不需要清除操作。当您到达构造函数的主体时,矢量对象已默认构造。默认构建的向量为空。

  2. 即调用默认基础class构造函数。如果不存在 base class 构造函数调用,则会自动调用默认值。仅当您希望调用基 class 的非默认构造函数时,才需要显式基 class 构造函数调用。因为您发布的代码正在调用默认构造函数,所以技术上不需要它。编译器会自动完成。

  3. 是的,而且通常不在构造函数体中进行初始化:

term_structure_class_interpolated::term_structure_class_interpolated(const vector<double>& in_times,
const vector<double>& in_yields)
  : times_(in_times),
    yields_(in_yields)
{
};

这会调用向量的复制构造函数而不是复制赋值运算符。这避免了自动生成对向量默认构造函数的调用,如果您在构造函数主体中执行赋值,编译器将插入该调用。

请注意,这样做意味着您不能简单地 return 当向量的大小不相等时,就像发布的代码所做的那样。如果需要,您需要使用复制赋值运算符在构造函数主体内分配向量。