【问题标题】:Odd behaviour when using Clang to build a C++ static lib使用 Clang 构建 C++ 静态库时的奇怪行为
【发布时间】:2013-12-22 05:57:01
【问题描述】:

我目前在最新的 OSX 上使用 Clang 在 C++11 中构建一个静态库,遇到了一些对我来说似乎很奇怪的东西。

我有从 A 和 B 继承的 C 类。A 用作该类用户的公共接口,B 用作抽象实现,保存所有 A 实例通用的方法。我来自Java 世界,所以我将 A 视为接口,将 B 视为抽象类。

在 C 类中,我有一个从 B 继承的方法,它调用 C 中定义的两个私有方法。这就是奇怪之处。如果我像这样实现继承的方法:

bool EPubParser::isCorrectFileType() {
  bool has_correct_file_type = false;
  bool is_compressed_file = this->isCompressedFile();
  if (this->hasRightExtension() && is_compressed_file) {
    has_correct_file_type = true;
  }

  if (has_correct_file_type) {
    LOG(DEBUG)<< "Has correct file type. " << endl;
  } else {
    LOG(DEBUG)<< "Does not have correct file type. " << endl;
  }
  return has_correct_file_type;
}

一切正常。我可以看到this-&gt;isCompressedFile() 中的日志语句被调用。如果我删除临时变量is_compressed_file 并在条件中直接调用this-&gt;isCompressedFile(),它们不会被调用。就好像方法不存在一样。

我错过了一些关于 C++ 的东西吗?这是一个 Clang 错误吗?我的 Java 背景是否让我感到困惑?

【问题讨论】:

    标签: c++ oop inheritance c++11 clang


    【解决方案1】:

    如果您直接调用isCompressedFile() 作为&amp;&amp; 运算符的第二个操作数,它只会在第一个操作数是true 时执行 - C++ 布尔运算符是短路的(但我相信这同样适用于Java)。

    【讨论】:

    • 第一个操作数为真。我查过了。
    • 我会收回的。我确实在第一个操作数中有一个错误。谢谢!
    猜你喜欢
    • 2013-03-25
    • 1970-01-01
    • 2011-12-31
    • 2023-03-18
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多