{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Type Names

Type Names - can still be used however by specifying it via...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Type Names 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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; union U u; enum E e; 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....
View Full Document

{[ snackBarMessage ]}

Page1 / 2

Type Names - can still be used however by specifying it via...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online