



(7 ratings)
In C, a common style of usage is to say:
struct A {
int x;
};
typedef struct A A;
after which A can be used as a type name to declare objects:
void f()
{
A a;
}
In C++, classes, structs, unions, and enum names are automatically type
names, so you can say:
struct A {
int x;
};
void f()
{
A a;
}
or:
enum E {ee};
void f()
{
E e;
}
By using the typedef trick you can follow a style of programming in C
somewhat like that used in C++.
But there is a quirk or two when using C++. Consider usage like:
struct A {
int x;
};
int A;
void f()
{
A a;
}
This is illegal because the int declaration A hides the struct
declaration. The struct A can still be used, however, by specifying
it via an "elaborated type specifier":
struct A
The same applies to other type names:
class A a;Taking advantage of this feature, that is, giving a class type and a variable or function the same name, isn't very good usage. It's supported for compatibility reasons with old C code; C puts structure tags (names) into a separate namespace, but C++ does not. Terms like "struct compatibility hack" and "1.5 namespace rule" are sometimes used to describe this feature.
union U u;
enum E e;
20 Random Tutorials from the same category :
Classes (I)
Operators in C++
Stream I/O Performance
Other Data Types
Operators
Bjarne Stroustrup's C++ Style and Technique FAQ 1
Operator New/Delete
Inline Functions
Variables. Data Types.
A Coding Convention for C++ Code
Operator Precedence in C++
Constructors and Integrity Checking
What is a Variable in C++ ?
Templates
"Hello, World" Program in C++
Control Structures
What is a Function in C++ ?
Constants
Declaration Statements
Function Overloading














