[英]C++ syntax question, use of the class keyword
Can any one explain "class X x;" 谁能解释“类X x;”? what actually it mean ...in the program...Please give me some example also? 在程序中实际上是什么意思...还请给我一些示例吗? Ex: 例如:
#include <iostream>
using namespace std;
class X {
public:
X() {}
};
class Y {
public:
Y() {}
};
int main()
{
class X x; //what is this? explain it
class Y y; //what is this? explain it
class Z z; //what is this? explain it //error
return 0;
}
Edited: May i know the exact difference between "Class Z z" and "Z z".Because While compiling this program in 2 ways 编辑:我可以知道“ Z Z类”和“ Z Z类”之间的确切区别。因为以两种方式编译该程序
class Z Z; //here iam getting as Error: error: incomplete type is not allowed
Z z; //error: identifier "Z" is undefined
Since iam asking the exact difference. 由于iam问确切的区别。
This is called an elborated type specifier and is useful in cases such as below: 这称为精化类型说明符,在以下情况下很有用:
$9.1/3 - "An elaborated-type-specifier (7.1.5.3) can also be used as a type-specifier as part of a declaration. It differs from a class declaration in that if a class of the elaborated name is in scope the elaborated name will refer to it." $ 9.1 / 3-“详细说明的类型说明符(7.1.5.3)也可以用作声明的一部分,作为类型说明符。它与类声明的不同之处在于,如果详细说明名称的类在范围内,详细的名称将引用它。”
int bad;
class bad{};
int main(){
//bad b;
class bad b; // Elaborated type specifier
}
You should now be able to undersand the difference between class Z z
and Z z
; 您现在应该能够理解class Z z
和Z z
class Z z
之间的区别;
AFAIK 据我所知
class X x;
is equivalent to 相当于
X x;
( EDIT: for those nitpickers: at least, in your case, when X is a previously defined class and there is no other variable named X in the same scope ). ( 编辑:对于那些nitpickers:至少在您的情况下,当X是先前定义的类,并且在同一作用域中没有其他名为X的变量时 )。
In plain-old C, whenever you define a struct
without a typedef
, you have explicitly use the struct
keyword when creating variables. 在普通老式C,只要你定义一个struct
没有typedef
,你已经明确地使用struct
创建变量时,关键字。 In C++, one can omit the struct
keyword, but you also can write struct X x;
在C ++中,可以省略struct
关键字,但是您也可以编写struct X x;
if you prefer that. 如果您愿意的话。 And since the only difference between class
and struct
in C++ is the default visibility, one can conclude that it is also legal to write class X x;
而且由于C ++中class
和struct
之间的唯一区别是默认可见性,因此可以得出结论,编写class X x;
也合法class X x;
Answer to your edit: 回答您的编辑:
class Z;
is a forward declaration (often used in header files where you don't want to #include "Z.hpp"
). 是一个前向声明(通常在您不想#include "Z.hpp"
头文件中使用)。 This is also called an incomplete type declaration . 这也称为不完整类型声明 。 It allows pointers to Z to be declared, for example 例如,它允许声明指向 Z的指针
class Z *z;
is legal code, even when the compiler has not seen any class body declaration of Z. What is not allowed is to create an instance of z like class Z z;
即使在编译器未看到Z的任何类主体声明的情况下,它也是合法的代码。不允许的是创建类似class Z z;
实例class Z z;
as long as Z is incomplete from the compiler's view. 只要Z从编译器的角度来看是不完整的。
The code Z z;
代码Z z;
, however cannot be interpreted as a forward declaration by the compiler (not even as a disallowed forward declaration). ,但是编译器不能将其解释为前向声明(甚至不能视为不允许的前向声明)。 It just shows up as "Z is undefined". 它只是显示为“ Z未定义”。
C++ started out as an extension of C. C ++最初是C的扩展。
In C class names are not full-fledged typenames. 在C类中,名称不是完整的类型名。 So in C, if you write 所以在C中,如果您编写
struct Blah
{
int x;
};
then to declare a variable of this class, in C you have to write 然后要声明此类的变量,必须在C中编写
struct Blah myBlahObject;
In C++ the class name is a full-fledge typename, so you can just write 在C ++中,类名是完整类型名,因此您可以编写
Blah myBlahObject;
But mainly for compatibility with C, C++ retains the C-style declaration syntax, and generalizes it to also work with the C++ class
keyword. 但是主要是为了与C兼容,C ++保留了C样式的声明语法,并将其泛化为也可以与C ++ class
关键字一起使用。
And that means that in C++ you can write eg 这意味着在C ++中,您可以编写例如
class NeverSeenBefore* p;
to declare a pointer variable p
of type NeverSeenBefore*
. 声明类型为NeverSeenBefore*
的指针变量p
。 Where NeverSeenBefore
has not been declared anywhere yet. 尚未在任何地方声明NeverSeenBefore
位置。 It's just an incomplete type , and use of the class
(or struct
) keyword informs the compiler, as in C, that there is such a type, even though it's not yet been declared. 它只是一个不完整的类型 ,并且使用class
(或struct
)关键字会通知编译器(如C中一样),即使尚未声明该类型。
C++ has a built in incomplete type, called void
-- and you get the same kind of error message (from a good compiler) if you try to declare a void
variable. C ++有一个内置的不完整类型,称为void
如果尝试声明一个void
变量,则会收到相同类型的错误消息(来自一个好的编译器)。
The main difference between void
and other incomplete types is that void
can not be completed. void
和其他不完整类型之间的主要区别在于, void
无法完成。
By the way, the C technique for obtaining a full fledged typename is to use typedef
. 顺便说一句,用于获取完整类型名的C技术是使用typedef
。 Since class names are full fledged typenames in C++, such typedef
is not necessary in C++. 由于类名在C ++中是完整的类型名,因此C ++中不需要这种typedef
。 So, when you see C++ code where a typedef
is provided for a class instead of just naming the class, then you know that a C programmer has been there – or a person taught by a C programmer (or recursively, taught by a person taught by a C programmer, so on). 因此,当您看到为类提供typedef
而不是仅给类命名的C ++代码时,您就会知道有C程序员在那儿–或由C程序员教过的人(或递归,由受教人教过的人)由C程序员编写,依此类推)。
Cheers & hth., 干杯,……
class X x;
instantiates an object of type class X
on the stack that is named x
. 实例化类型的对象class X
名为堆栈上x
。 Looking at your definition of X
there are no methods or fields, so you really can't do much with it. 查看您对X
的定义,没有方法或字段,因此您实际上不能做太多事情。
Do you know what classes are in c++? 您知道c ++中有哪些类吗?
声明类的实例时, class
关键字是可选的。
As per your new edit, 根据您的新修改,
class Z z;
when the compiler encounter this, it identifies that Z
is a class since you specified it. 当编译器遇到此问题时,由于指定了Z
,因此它标识Z
是一个类。 But it cannot find the declaration of the class Z
. 但是它找不到class Z
的声明。 Hence it is showing error: incomplete type is not allowed
. 因此,它显示error: incomplete type is not allowed
。
Now, when the compiler encounters Z z;
现在,当编译器遇到Z z;
it will not have any idea about what Z
is since you didn't specify it anywhere. 因为您没有在任何地方指定Z
,所以对Z
是什么一无所知。 So it reports error: identifier "Z" is undefined
. 因此它报告error: identifier "Z" is undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.