2

I am currently using Clang on the latests OSX to build a static library in C++11 and came across something that seems odd to me.

I have class C that inherits from A and B. A is used as a public interface for the users of the class and B is used as an abstract implementation, holding methods that are common to all instances of A. I come from the Java world so I'm treating A as an interface and B as an abstract class.

In class C I have a method that is inherited from B that calls two private methods defined in C. And herein lies the weirdness. If I implement the inherited method like this:

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

everything works fine. I can see log statements in this->isCompressedFile() being called. If I remove the temporary variable is_compressed_file and call this->isCompressedFile() directly in the condition, they don't get called. It's as if the method doesn't exist.

Am I missing something about C++? Is this a Clang bug? Is my Java background tripping me up?

1 Answer 1

5

If you call isCompressedFile() directly as the second operand of the && operator, it will only be executed if the first operand is true - C++ boolean operators are short-circuiting (but I believe the same applies to Java).

Sign up to request clarification or add additional context in comments.

2 Comments

The first operand is true. I've checked.
I'll take that back. I did have a bug in the first operand. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.