In C , it is not possible to directly define member functions that are both static and virtual. The compiler will issue an error when attempting to declare a "static virtual member()". However, there are techniques to achieve an equivalent functionality.
To emulate the behavior of a static virtual member function, consider the following approach:
<code class="cpp">struct Object { struct TypeInformation; static const TypeInformation &GetTypeInformation() { return GetTypeInformationImpl(); } protected: virtual const TypeInformation &GetTypeInformationImpl() const = 0; };</code>
Here, the GetTypeInformation() function is defined as static and returns a constant reference to the TypeInformation type. However, the actual implementation of this function is delegated to the derived class via the protected virtual function GetTypeInformationImpl().
The above is the detailed content of Can Static Member Functions Be Virtual in C ?. For more information, please follow other related articles on the PHP Chinese website!