typedef
What this keyword exactly does? It's work is like pre-proccessor directives, but it does not have "#" prefix!!
typedef
What this keyword exactly does? It's work is like pre-proccessor directives, but it does not have "#" prefix!!
typedef defines a user-defined type. So:defines a new type, ULLong, that can be used in variable and member declarations. typedef can be used to define both simple types and structured types, such as inCode:typedef unsigned long long ULLong;which defines a new type called tabent_t that can be used where you might otherwise use struct tabent_s. The namespace for structs and user-defined types is separate in C, so only one type is defined in the above example, but in C++, defining the named struct automatically defines a type of the same name, so in C++ the typedef in the above example defines two identical types, tabent_s and tabent_c.Code:typedef struct tabent_s { UULong value; char name[200]; } tabent_t;
You can think of typedef as a short-hand, similar to #define, but it's not handled by the pre-processor, and the compiler can do strong type checking on user defined types, thus making the code more portable and robust.
Last edited by filker0; 01-24-2006 at 08:27 AM.
Insert obnoxious but pithy remark here
Thanks
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
long long var?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Actually, typedefs aren't new types, they're just aliases. In other words:
There is absolutely no type conversion going on here, not even implicit. Typedefs could, in many cases, be replaced using search&find (or by the preprocessor):Code:typedef int myint; myint mi = 3; int i = mi; mi = i;
See also §7.1.3 in the C++ standard.Code:void func(int i); void func(myint i); // Overloading error: signatures aren't different.
So why not just use the preprocessor? Because a typedef is aware of being a C++ construct. The most important case here are pointers. You probably know about the dangers of this syntax:
a is a pointer, but b is not! b is just a plain int.Code:int* a, b;
Now suppose you introduce a preprocessor macro pint as a pointer to int:
This works fine for a time:Code:#define pint int*
But it will fail for this, because the preprocessor only does text substitution.Code:pint a; pint b;
Again, (but far less obvious even for an experienced coder), a is a pointer but b is not. That's because the code after expansion looks again like this:Code:pint a, b;
Typedefs are aware of C++ syntax. A typedef'd name is a unit and cannot be taken apart:Code:int* a, b;
Both a and b are pointers here.Code:typedef int *pint; pint a, b;
A similar case:
What are the types of a and b?Code:#define mpint int* typedef int *tpint; const mpint a; const tpint b;
a is a pointer to const int. b is a const pointer to int.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law