Difficulty: Easy
Correct Answer: It is not allowed.
Explanation:
Introduction / Context:
In everyday C++ practice, classes are named user-defined types that you can declare, define, and instantiate. Anonymous types exist for certain constructs (like unnamed unions in some contexts), but ordinary class declarations are expected to have an identifier when intended for general use. This question probes whether nameless class declarations are permitted in the usual sense presented in introductory curricula and MCQs.
Given Data / Assumptions:
Concept / Approach:
Standard C++ codebases define classes with names so they can be referenced, instantiated, and used in interfaces. While certain advanced patterns and special cases exist (e.g., unnamed classes used inline as members in some compilers), they are not part of common, portable style. Thus, the practical and pedagogical answer is that a class “without any name” is not allowed in the ordinary sense of defining a reusable type. Therefore, among the provided options, “It is not allowed.” is the best answer.
Step-by-Step Solution:
Verification / Alternative check:
Compilers will reject most attempts to define a top-level anonymous class meant for instantiation as a type. Practical examples in teaching material uniformly present named classes. Anonymous aggregates are a separate topic and not what this question targets.
Why Other Options Are Wrong:
“Cannot have a constructor” or “cannot have a destructor” are red herrings; the core issue is the unusability/invalidity, not specific member availability.
“Both A and B” accumulates two incorrect premises.
Common Pitfalls:
Final Answer:
It is not allowed.
Discussion & Comments