'class' keyword in variable definition in

2019-04-04 06:06发布

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!

3条回答
不美不萌又怎样
2楼-- · 2019-04-04 06:45

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

查看更多
forever°为你锁心
3楼-- · 2019-04-04 06:47

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.

查看更多
Luminary・发光体
4楼-- · 2019-04-04 07:02

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.

查看更多
登录 后发表回答