c++ - 构造函数体中的对象初始化

标签 c++ oop

我已经编写了一个类来根据模式执行存储在 unordered_map 中的数据的输出。我希望此模式在对象创建时传递给对象,并在对象生命周期内保持不变。

这是我先写的类:

class Format {
public:
  Format(const string output_pattern);
  string interpolate(const boost::unordered_map<string, string> & field_values);
private:
  ...
};

我想以这种方式在另一个类中使用 Formatter:

class A {
private:
  Format formatter;
public:
  A(const std::map<std::string, std::string> & info, ... other parameters) {
    std::map<std::string, std::string>::const_iterator it;
    it = info.find("format");
    if (it == info.end()) {
      throw std::runtime_error("Can't find 'format' in info");
    } else {
      formatter = Format(it->second);
    }
  }
};

如您所见,在调用构造函数之前还有一些工作要做。毫不奇怪,它不起作用,因为 format 首先使用默认构造函数(缺少)初始化,其次它期望为 formatter = Format(it->second); 定义的赋值运算符; 行。

我不能像这样初始化formatter:

A(const std::map<std::string, std::string> & info, ... other parameters):
  formatter(...) {
  ...
}

因为我必须首先提取参数以作为 formatter 初始化程序传递。

所以,最终我是这样解决的:

class Format {
public:
  Format();
  void set_pattern(const string output_pattern);
  string interpolate(const boost::unordered_map<string, string> & field_values);
private:
  ...
};


class A {
private:
  Format formatter;
public:
  A(const std::map<std::string, std::string> & info, ... other parameters):
    formatter()
 {
    std::map<std::string, std::string>::const_iterator it;
    it = info.find("format");
    if (it == info.end()) {
      throw std::runtime_error("Can't find 'format' in info");
    } else {
      formatter.set_pattern(it->second);
    }
  }
};

我真正不喜欢的是它把不可变的 formatter 对象变成了可变的。这不是一个大问题。但我不需要它是可变的,而且我不喜欢它,因为当它是不可变的时候我不能用代码表达它而不得不让它可变。

在我的例子中,有没有其他好的方法可以在构造函数中初始化它?

最佳答案

您可以使用辅助私有(private)函数来完成实际工作:

class A {
private:
  SomethingA before;
  Format formatter;
  SomethingB after;
public:
  A(const std::map<std::string, std::string>& info) :
    formatter(extractFormat(info))
  {
    // ...
  }
private:
  string extractFormat(const std::map<std::string, std::string>& info)
  {
    // Be careful here! The object is not constructed yet, you can't fully use it
    // this->before => ok
    // this->after => not ok
    // you could make this function static just to be safe
  }
};

关于c++ - 构造函数体中的对象初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20284212/

相关文章:

c++ - 未格式化的输入到 std::string 而不是来自二进制文件的 c-string

c# - 如何避免 MediatR 请求处理程序中的代码重复?

javascript - Object 返回其构造函数而不是 JS 中的 false。为什么?

php - 族的数据库设计以及如何插入数据库

c++ - 我应该在类里面使用 setters/getters 吗?

c++ - 从指针到数组的类型转换

c++ - 如何在 Mac OS X (C++) 中使用 dylib

c++ - 现代 C++ 设计中的 CompileTimeChecker 未按预期工作

C++ 试图创建一个 'Intermediate' 仿函数

oop - 领域模型和对象模型