Home >Backend Development >C++ >What is an Injected Class Name and How Has Its Use Evolved?
Injected Class Name: Purpose and Historical Evolution
The notion of an injected class name refers to the implicit declaration of a class within its own scope. This allows the class name to be used within the class body to refer to the class itself, even in the presence of other entities with the same name in the enclosing scope.
Purpose of the Injected Class Name
The injected class name serves several practical purposes, including:
Historical Introduction
The concept of an injected class name existed in some form even before C . The ARM (Ada Reference Manual) specified that the class name could be used within the class specifier to refer to the class itself.
In C 98, the injected class name was implicitly inserted into the scope of the class. However, the terminology "injected-class-name" was introduced in C 03 with DR 147.
Example Use Case
Consider the following class:
class X { public: static X create() { return X(); } };
Without the injected class name, it would be unclear whether create() creates a temporary X object or calls the function X. The injected class name ensures that within the body of X, the name X always refers to the class itself, disambiguating the intent of create().
The above is the detailed content of What is an Injected Class Name and How Has Its Use Evolved?. For more information, please follow other related articles on the PHP Chinese website!