1

What would be the value of Field.Format("%04d", ErrorCode) in the procedure below if the AErrorCode is ERR_NO_HEADER_RECORD_FOUND_ON_FILE?

Somewhere in a .h file:

enum AErrorCode
{
    ERR_UNKNOWN_RECORD_TYPE_CODE = 5001,
    ERR_NO_HEADER_RECORD_FOUND_ON_FILE,
    ERR_DUPLICATE_HEADER_RECORD_FOUND,

    ERR_THIRD_PARTY_LETTER_RECORD_HAS_A_ZERO_REFERRAL_AMOUNT = 5101,    

    ERR_CALL_OCA_UNKNOWN_PROBLEM = 5999
};

In some procedure:

void TADataset::SetErrorStatus(AErrorCode ErrorCode)
{
    NDataString Field;
    Field.Format("%04d", ErrorCode);
    AckRecord.SetField("oca_error_stat", "E");
    AckRecord.SetField("error_cd", Field);
}
1
  • To be pedantic, you should have a cast as in Field.Format("%04d", static_cast<int>(ErrorCode));. Technically, enums don't have to be the same size as an int, though they typically are in most implementations. Commented Sep 17, 2009 at 20:58

2 Answers 2

4
 ERR_NO_HEADER_RECORD_FOUND_ON_FILE == 5002

If you don't specify any value at all, it starts at 0 and increments the next element in the enum. If you specify a value, then it starts incrementing starting by the next element. Unless you reset the counter again by specifying another value for a successor element.

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

2 Comments

It increments using whatever was previously defined + 1.
starts with 0, increments of 1, unless you specify the next value, then continue incrementing
2

According C++ Standard 7.2/1:

<...>If the first enumerator has no initializer, the value of the corresponding constant is zero. An enumerator-definition without an initializer gives the enumerator the value obtained by increasing the value of the previous enumerator by one.

It means that ERR_NO_HEADER_RECORD_FOUND_ON_FILE equal to ERR_UNKNOWN_RECORD_TYPE_CODE+1.

Comments

Your Answer

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