【问题标题】:Do we need to clear a class member of type vector in beginning of the constructor function of the class?我们是否需要在类的构造函数的开头清除向量类型的类成员?
【发布时间】:2021-05-25 03:38:16
【问题描述】:

我正在尝试了解此 pdf 中类的类定义: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

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


#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() ,据我了解,这使类成员向量的大小为零。我不明白为什么要这样做?它们的大小不是已经为零了吗?

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

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;

};

【问题讨论】:

  • times_.erase(times_.begin(), times_.end()); -- 编写此代码的人要么使用了一些非常旧的编译器,要么他们不知道他们可以简单地这样做:times_.clear();
  • 您的复制/粘贴操作是否删除了一些下划线?
  • 是的,我就是这么想的。但我不明白为什么需要这个功能?也许不是,作者不必要地使用了它?
  • 那些不是最明显的。 class term structure class interpolated 很难看。
  • 旁注:_TERM_STRUCTURE_CLASS_INTERPOLATED_an illegal identifier。保留下划线后跟大写字母。被这个错误绊倒是非常罕见的,但是当它发生时它是绝对的想法,因为由此产生的错误几乎是难以理解的

标签: c++ class vector stl stdvector


【解决方案1】:
  1. 不需要清除操作。当您到达构造函数的主体时,向量对象已被默认构造。默认构造的向量为空。

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

  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)
{
};

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

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

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2014-08-04
    • 2022-06-15
    • 2015-05-23
    相关资源
    最近更新 更多