I came across a piece of code that looked like this:
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
What does the "class" keyword do on the return type? I can't find anywhere that explains what it's function is. Does it just specify that it's talking about SomeClass as a class in case there is some sort of ambiguousness or something? I am confused.
class SomeClass
is a longhand way of referring to the class type SomeClass
(technically, it's the elaborated type specifier). Usually, adding class
is redundant, and the two are equivalent. But it's sometimes necessary to resolve the ambiguity, if there's a variable or function with the same name.
It is used to disambiguate.
Say for example if you have a variable of the same name in the same (or outer) scope, something like this:
int SomeClass; //SomeClass is declared to be variable here
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
Without the class
keyword, the function declaration wouldn't make sense to the compiler. The class
keyword tells the compiler to ignore the variable declaration, and look for a class declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With