Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"class" Keyword on Return Type - C++ [duplicate]

Tags:

c++

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.

like image 813
Mic Rooney Avatar asked Apr 20 '15 17:04

Mic Rooney


2 Answers

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.

like image 73
Mike Seymour Avatar answered Nov 08 '22 17:11

Mike Seymour


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.

like image 14
Nawaz Avatar answered Nov 08 '22 17:11

Nawaz