25

Before anyone asks, yes, this is part of a homework, and yes, I did a lot of Googling before asking. I spent the last hour searching intensively on Google with many, many different keywords, but just could not find anything.

So here goes the question :

What does the following variable definition mean: class MyClass* myClass;?

I tried that code with something like class MyClass* myClass = new MyClass(); and found that it simply creates a pointer to a new instance of MyClass.

So, what's the advantage of using the class prefix? Does it make any difference?

Does someone have a link to some resources about it? I simply could not find anything (it's really, really hard to find things other than "class definition"!).

Thanks a lot!

2
  • Can you give some context of where you saw this? Was it a member variable in a class? Seems to me like it could be a forward declaration combined with variable declaration.
    – Borgleader
    Jan 18, 2015 at 4:36
  • 1
    You would almost never need it. One obscure case is when the class name is hidden by a variable or function: class MyClass {}; int MyClass; MyClass* myClass = NULL; (won't compile, but you can add class keyword to refer to an otherwise hidden name). Jan 18, 2015 at 4:38

3 Answers 3

26

An elaborated type specifier is a type name preceded by either the class, struct, enum, or union keyword.

class identifier 
struct identifier 
enum identifier 
union identifier

An elaborated type specifier is used either for emphasis, or to reveal a type name that is hidden by the declaration of a variable with the same name in the same scope.

source

1
  • 1
    Plus 1 for mentioning elaborated type specifiers
    – namezero
    Jan 18, 2015 at 8:49
16

Actually it is optional to use class while creating object of class. In C language it is compulsory to use struct in front of struct name to create its variable. As C++ is superset of C. There is only one difference between struct and class in C++, and that is of access modifier.

To keep backward compatible it is permissible.

So,

class MyClass* myClass = new MyClass();

And,

MyClass* myClass = new MyClass();

Both are same.

1
  • 4
    C++ is almost a superset of C. Also, your answer originally referred to C in the past tense; C still exists, and the rules in this area are unchanged. Feb 25, 2015 at 19:13
11

The words "inline forward declaration" need to be mentioned here!

A forward declaration is simply a way to tell the compiler about a type name before its actually defined. You find these in header files all the time.

// MyStruct.h
class MyClass;

struct MyStuct {
   MyClass* m_myClass;
   void foo();
}

// MyStruct.cpp
#inlude "MyClass.h"
void MyStruct::foo() { m_myClass->SomeFunc(); }

Notice that the header file only declares MyClass as a class identifier, and has no idea what it actually is until its defined via the #include in the cpp file. This is forward declaration.

inline forward declaration is really the same thing, but you simply do it all on one line. This is the same exact code achieving the same thing.

// MyStruct.h
struct MyStuct {
   class MyClass* m_myClass;
   void foo();
}

// MyStruct.cpp
#inlude "MyClass.h"
void MyStruct::foo() { m_myClass->SomeFunc(); }

I feel like most programmers prefer the standard forward declaration method (less typing overall usually). Which is why it can be confusing when stumbling upon the lesser used inline version.

I see many of the answers here calling it an optional keyword, but in the above context of inline forward declaration, it is very much not optional, and will result in compile errors due to missing type specifier.

1
  • 1
    The C++ standard doesn't use the terms "forward declaration" or "inline forward declaration". The standard terms are "declaration" and "elaborated type specifier" , as mentioned elsewhere on this thread. Also, maybe you misread the other answer which says "Actually it is optional to use class while creating object of class" -- in order to create an object, the type must be complete, and it is indeed optional to use class given that the type is complete already.
    – M.M
    May 24, 2017 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.