1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Before C++17, a static const int member with an in-class…

Added 30 Nov -0001

Before C++17, a static const int member with an in-class initialiser still required what, if it was odr-used?

  1. A second declaration inside main, because the initialiser was not visible outside the class scope
  2. An out-of-class definition, because the in-class initialiser served only as a declaration for such uses Correct answer
  3. A definition in every translation unit that included the header, to satisfy the one-definition rule
  4. Nothing, since in-class initialisers always provided a full definition that the linker was normally able to use

Correct answer

B. An out-of-class definition, because the in-class initialiser served only as a declaration for such uses

16 views

MCQSEXAM

Explanation

odr-use, meaning taking the address or binding a reference, required a separate definition such as const int T::k; outside the class, even though the in-class initialiser allowed use in constant expressions. C++17 introduced inline variables, so a static constexpr member is now implicitly inline and self-defining. Forgetting the definition produced the classic undefined reference linker error.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs