什么是 'class' in 'class DataType* Variable' in Unreal Engine Shooter Game Sample
what is 'class' in 'class DataType* Variable' in Unreal Engine Shooter Game Sample
我最近下载了适用于 Unreal 4 Engine 的 Shooter Game,我只是想拆开 c++,但我的 c++ 不是最好的,我注意到一个名为
的变量
class AShooterCharacter* MyPawn;
在头文件中设置为ShooterWeapon.h
我想了解 class
部分是什么。
[编辑]
我注意到人们拒绝了我的问题,所以我将其改为一个问题。我希望人们愿意提供帮助而不是贬低我的问题。没有愚蠢的问题:)...尤其是在编程中
来个小介绍(来自Wikipedia)
A class is a user defined type or data structure declared with keyword
class that has data and functions (also called methods) as its members
whose access is governed by the three access specifiers private,
protected or public (by default access to members of a class is
private). A class (declared with keyword class) in C++ differs from a
structure (declared with keyword struct) as by default, members are
private in a class while they are public in a structure. The private
members are not accessible outside the class; they can be accessed
only through methods of the class. The public members form an
interface to the class and are accessible outside the class. Instances
of these data types are known as objects and can contain member
variables, constants, member functions, and overloaded operators
defined by the programmer.
一个class用于实现面向对象编程(OOP)
通过将关键字 class
放在标识符之前,它就变成了一种数据类型(如 int 或 char),但好处是您可以决定其中包含的所有内容。例如
class A
{
int num;
char ch;
public:
void getvalue ( )
{
std::cin >> num >> ch ;
}
void disp ( )
{
std::cout << num << std::endl << ch ;
}
};
现在 A
是 class 而当你这样做时
A myobject ;
myobject
成为 class A
的对象,也就是说,它成为可以存储 int
和 char
值的数据类型(您也可以添加任何其他数据类型,例如 float)。
你可以调用函数getvalue()
来获取num
和ch
的值,然后输出像
这样的值
myobject.getvalue();
myobject.disp()
如果 AShooterCharacter
已经在范围内,那么它可能基本上没有任何意义。
class AShooterCharacter* MyPawn;
// ^ the same as just:
// AShooterCharacter* MyPawn;
在 C 中,命名结构类型时,必须使用 struct
关键字:
struct Foo
{
int x, y, z;
};
struct Foo obj;
在 C++ 中,您不需要这样做,因为 Foo
本身就是一个可命名的类型:
Foo obj;
但是如果你愿意,你仍然可以写struct Foo
。
这同样适用于 class
,这是语言语法和语义定义方式的结果。
只有两次不同。
用法 1:消除歧义(某种)
您可以使用它来指定您要引用范围内的类型名称,否则它会被其他名称隐藏,例如:
class Foo {};
int main()
{
const int Foo = 3;
// Foo x; // invalid because `Foo` is now a variable, not a type
class Foo x; // a declaration of a `Foo` called `x`;
}
但是如果你发现自己"needing"这样的话,在我看来,你的问题就更大了!
用法 2:前向声明
否则等同于这样做:
class Foo; // a forward declaration
Foo* x;
如果您要转发声明类型并立即声明指向该类型对象的指针,可以说它节省了一行代码。
不过这不是常见的风格。
这就是所谓的"forward declaration"。
它允许您指定指向 class/struct 名称的指针,这样您就不必包含定义 class/struct.
的特定头文件
此功能在打破头文件之间的循环依赖性时特别可行。
您可以通过检查 AShooterCharacter
是否未在 ShooterWeapon.h
包含的任何文件中定义来验证是否属于这种情况。
我最近下载了适用于 Unreal 4 Engine 的 Shooter Game,我只是想拆开 c++,但我的 c++ 不是最好的,我注意到一个名为
的变量class AShooterCharacter* MyPawn;
在头文件中设置为ShooterWeapon.h
我想了解 class
部分是什么。
[编辑] 我注意到人们拒绝了我的问题,所以我将其改为一个问题。我希望人们愿意提供帮助而不是贬低我的问题。没有愚蠢的问题:)...尤其是在编程中
来个小介绍(来自Wikipedia)
A class is a user defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private). A class (declared with keyword class) in C++ differs from a structure (declared with keyword struct) as by default, members are private in a class while they are public in a structure. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of these data types are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.
一个class用于实现面向对象编程(OOP)
通过将关键字 class
放在标识符之前,它就变成了一种数据类型(如 int 或 char),但好处是您可以决定其中包含的所有内容。例如
class A
{
int num;
char ch;
public:
void getvalue ( )
{
std::cin >> num >> ch ;
}
void disp ( )
{
std::cout << num << std::endl << ch ;
}
};
现在 A
是 class 而当你这样做时
A myobject ;
myobject
成为 class A
的对象,也就是说,它成为可以存储 int
和 char
值的数据类型(您也可以添加任何其他数据类型,例如 float)。
你可以调用函数getvalue()
来获取num
和ch
的值,然后输出像
myobject.getvalue();
myobject.disp()
如果 AShooterCharacter
已经在范围内,那么它可能基本上没有任何意义。
class AShooterCharacter* MyPawn;
// ^ the same as just:
// AShooterCharacter* MyPawn;
在 C 中,命名结构类型时,必须使用 struct
关键字:
struct Foo
{
int x, y, z;
};
struct Foo obj;
在 C++ 中,您不需要这样做,因为 Foo
本身就是一个可命名的类型:
Foo obj;
但是如果你愿意,你仍然可以写struct Foo
。
这同样适用于 class
,这是语言语法和语义定义方式的结果。
只有两次不同。
用法 1:消除歧义(某种)
您可以使用它来指定您要引用范围内的类型名称,否则它会被其他名称隐藏,例如:
class Foo {};
int main()
{
const int Foo = 3;
// Foo x; // invalid because `Foo` is now a variable, not a type
class Foo x; // a declaration of a `Foo` called `x`;
}
但是如果你发现自己"needing"这样的话,在我看来,你的问题就更大了!
用法 2:前向声明
否则等同于这样做:
class Foo; // a forward declaration
Foo* x;
如果您要转发声明类型并立即声明指向该类型对象的指针,可以说它节省了一行代码。
不过这不是常见的风格。
这就是所谓的"forward declaration"。
它允许您指定指向 class/struct 名称的指针,这样您就不必包含定义 class/struct.
的特定头文件此功能在打破头文件之间的循环依赖性时特别可行。
您可以通过检查 AShooterCharacter
是否未在 ShooterWeapon.h
包含的任何文件中定义来验证是否属于这种情况。