C++ syntax question, use of the class keyword
Can any one explain "class X x;" what actually it me开发者_开发技巧an ...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
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.
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."
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
;
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).
In plain-old C, whenever you define a struct
without a typedef
, you have explicitly use the struct
keyword when creating variables. In C++, one can omit the struct
keyword, but you also can write 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;
Answer to your edit:
class Z;
is a forward declaration (often used in header files where you don't want to #include "Z.hpp"
). This is also called an incomplete type declaration. It allows pointers to Z to be declared, for example
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;
as long as Z is incomplete from the compiler's view.
The code 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".
C++ started out as an extension of C.
In C class names are not full-fledged typenames. So in C, if you write
struct Blah
{
int x;
};
then to declare a variable of this class, in C you have to write
struct Blah myBlahObject;
In C++ the class name is a full-fledge typename, so you can just write
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.
And that means that in C++ you can write e.g.
class NeverSeenBefore* p;
to declare a pointer variable p
of type NeverSeenBefore*
. Where NeverSeenBefore
has not been declared anywhere yet. 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.
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.
The main difference between void
and other incomplete types is that void
can not be completed.
By the way, the C technique for obtaining a full fledged typename is to use typedef
. Since class names are full fledged typenames in C++, such typedef
is not necessary in C++. 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).
Cheers & hth.,
class X x;
instantiates an object of type class X
on the stack that is named x
. Looking at your definition of X
there are no methods or fields, so you really can't do much with it.
Do you know what classes are in c++?
When declaring an instance of a class, the class
keyword is optional.
As per your new edit,
class Z z;
when the compiler encounter this, it identifies that Z
is a class since you specified it. But it cannot find the declaration of the class Z
. Hence it is showing error: incomplete type is not allowed
.
Now, when the compiler encounters Z z;
it will not have any idea about what Z
is since you didn't specify it anywhere. So it reports error: identifier "Z" is undefined
.
精彩评论