(7 ratings)   
By: computers,programming,c,language,object,pointer,coder,code,buyer
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++...
Added: 13 June 2008    Views: 480  
PathComputers    Programming    C++
Keywords: computers   programming   c   language   object   pointer   coder   code   buyer  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 

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;

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.
About the Author :
Type Names In C++
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments