Ace Your C++ Skills 2025 – Rock the ‘Thinking in C++’ Challenge!

Image Description

Question: 1 / 565

Why can't local classes contain static data members?

Static members can only be defined at file scope, and local classes don't allow for external definition

Local classes are classes defined within a function or block, and they have restricted access to everything outside of their scope. This means that static members, which belong to the class and not an object instance, cannot be declared within a local class. Option A is the correct answer because it accurately explains why static data members cannot be included in local classes - they can only be defined outside of the class, at file scope. Option B is incorrect because the C++ standard does not explicitly forbid it, though it is not allowed due to scoping rules. Option C is also not correct because while static members in local classes can be private, that is not the reason why static data members cannot be included. Option D is incorrect because local classes are not automatically static, and therefore cannot contain static data members.

Get further explanation with Examzify DeepDiveBeta

The C++ standard explicitly forbids it

They can, but static members must be private

Because local classes are automatically static

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy