【问题标题】:Good way to call overloaded const vs non-const method in C++ TDD?在 C++ TDD 中调用重载 const 与非 const 方法的好方法?
【发布时间】:2016-07-29 01:06:33
【问题描述】:

全部

编写了一些带有迭代器的精美容器。所以我有

iterator begin() {
}

iterator end() {
}

const_iterator begin() const {
}

const_iterator end() const {
}

在测试中我实例化容器,填充并测试它。调用 const 和非 const 版本的公认/好方法是什么?比如说,像这样的简单测试

TEST( c.end() - c.begin() == c.size() );

应该为迭代器和 const 迭代器运行。 常量参考?一些丑陋的演员?

【问题讨论】:

    标签: c++ unit-testing tdd catch-unit-test


    【解决方案1】:

    我只想对它做一个 const&:

    const container& constC= c;
    TEST(constC.end() - constC.begin() == constC.size());
    

    【讨论】:

      【解决方案2】:

      根据std 约定,我认为不重载它们是更好的解决方案。

      改为声明 cbegin()cend()

      【讨论】:

      • std 约定是执行上述所有操作:iterator begin();const_iterator begin() const;const_iterator cbegin() const;
      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      相关资源
      最近更新 更多