类声明混乱-右括号和分号之间的名称 [英] Class declaration confusion - name between closing brace and semi-colon
问题描述
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area (void);
} rect;
在此示例中,在该类定义中,右括号后和分号之间的"rect"是什么意思?我很难找到明确的解释.另外:不管是什么,您也可以为结构体做吗?
In this example, what does 'rect' after the closing brace and between the semi-colon mean in this class definition? I'm having trouble finding a clear explanation. Also: Whatever it is, can you do it for structs too?
推荐答案
rect
是变量的名称(在这种情况下为对象).
rect
is the name of a variable (an object in this case).
就像说过的话一样
int rect;
除了代替 int
之外,还有一个新类型的定义,称为 CRectangle
.通常,类类型是分别声明的,然后用作
except instead of int
there is a definition of a new type, called a CRectangle
. Usually, class types are declared separately and then used as
CRectangle rect;
您可能很熟悉,但是在这样的声明中声明一个新类型是完全合法的.
as you are probably familiar with, but it's perfectly legal to declare a new type as part of a declaration like that.
是的,它适用于结构:
struct SRectangle { int x, y; } rect;
实际上,如果您不打算再次使用该类型,则甚至不必给该类型起一个名字:
In fact you don't even have to give the type a name if you don't plan to use it again:
struct { int x, y; } rect;
这被称为匿名结构"(它也适用于类).
that's called an "anonymous struct" (and it also works for classes).
这篇关于类声明混乱-右括号和分号之间的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!