How many copies of a static data member exist for a C++ class with several objects instantiated?
Correct answer
D. Exactly one, shared by every object of the class
13 views
D. Exactly one, shared by every object of the class
13 views
A static data member belongs to the class rather than to any instance, so all objects share a single copy, which is ideal for counters and configuration shared across instances. Before C++17 it also needed a separate out-of-class definition, which is why headers often pair a declaration with a definition in one source file. Its static member function counterpart can be called without any object and has no this pointer.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.