Can any one explain “class 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
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.
AFAIK
is equivalent to
(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
structwithout atypedef, you have explicitly use thestructkeyword when creating variables. In C++, one can omit thestructkeyword, but you also can writestruct X x;if you prefer that. And since the only difference betweenclassandstructin C++ is the default visibility, one can conclude that it is also legal to writeclass X x;Answer to your edit:
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 exampleis 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”.