This is one of the differences between C and C++.
In C++, when you define a class you can use the type name with or without the keyword class
(or struct
).
// Define a class.
class A { int x; };
// Define a class (yes, a class, in C++ a struct is a kind of class).
struct B { int x; };
// You can use class / struct.
class A a;
struct B b;
// You can leave that out, too.
A a2;
B b2;
// You can define a function with the same name.
void A() { puts("Hello, world."); }
// And still define an object.
class A a3;
In C, the situation is different. Classes do not exist, instead, there are structures. You can, however, use typedefs.
// Define a structure.
struct A { int x; };
// Okay.
struct A a;
// Error!
A a2;
// Make a typedef...
typedef struct A A;
// OK, a typedef exists.
A a3;
It is not uncommon to encounter structures with the same names as functions or variables. For example, the stat()
function in POSIX takes a struct stat *
as an argument.