Junior — Middle
How to identify and use a method belonging to a specific class without creating its instance?
sobes.tech AI
Answer from AI
In C/C++, the keyword static is used to define a method belonging to a specific class that can be called without creating an instance.
A static method belongs to the class rather than an object and can be called using the class name.
Example:
class MyClass {
public:
static void staticMethod() {
// method code
std::cout << "Calling static method" << std::endl;
}
};
int main() {
MyClass::staticMethod(); // call without creating an object
return 0;
}
Thus, to define and use such a method, declare it with static and call it through the class name.