Home >Backend Development >C++ >Can C Members Be Both Static and Virtual?
Can C Members Be Both Static and Virtual?
In C , members cannot be declared both static and virtual. Compiling a declaration like static virtual member(); will result in an error.
However, you can achieve a similar effect using the following methods:
Here's an example:
<code class="cpp">struct Object { static const TypeInformation& GetTypeInformation(); virtual const TypeInformation& GetTypeInformation() const; }; struct SomeObject : public Object { static const TypeInformation& GetTypeInformation(); virtual const TypeInformation& GetTypeInformation() const override; };</code>
This allows you to call GetTypeInformation() both on objects (object->GetTypeInformation()) and on classes (SomeObject::GetTypeInformation()). Object::GetTypeInformation() will return the base class implementation, while SomeObject::GetTypeInformation() will call the overridden version.
The above is the detailed content of Can C Members Be Both Static and Virtual?. For more information, please follow other related articles on the PHP Chinese website!