What does the keyword 'typedef' do and what are the adavantages of using it in object oriented design?
Ex:
What does
mean? Thanks all.Code:. . . public: typedef B bucket_type; typedef K key_type;
What does the keyword 'typedef' do and what are the adavantages of using it in object oriented design?
Ex:
What does
mean? Thanks all.Code:. . . public: typedef B bucket_type; typedef K key_type;
There are no advantages in OOP for using typedef
Here is an example of why you would used typedef with a struct.
Code:struct car { char make[40]; int year; } my_car, mycar2; struct car whatever; typedef struct { char make[40]; int year; } my_car; my_car VW; my_car your_car;
-MethodMan-
Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.
Homepage: http://www.freewebs.com/andy_moog/home.html
But I still don't know what typedef means? Could somebody clarify it a bit further? Thanks.
It creates a type alias. For example
typedef int int_32; makes int_32 an alias to int.
It doesn't create a new type.
Nick, is correct. It is just an alias, it does NOT create a new data type.
Mr. C: Author and Instructor
would this function any differently ( from the programmers point of view ) than using a preprocessor definiton,
like would
typedef int int_32;
sort of work the same as
#define int_32 int
Just wondering because I have never used typedef before
Yes, they are similar in function.
Mr. C: Author and Instructor
Not true at all! There are still many uses of typedef in OOP.Originally posted by MethodMan
There are no advantages in OOP for using typedef
For one, simplification of names when dealing with a long datatypes, especially with templating. Also, for simplifying things such as the datatype:
constant pointer to a volatile cost pointer to class "DMSegment" const member function with return type ErrorEnum that takes parameters const char* and AudioFlags called "Accessor".
ErrorEnum ( DMSegment::* const volatile * const Accessor )( const char*, AudioFlags ) const
Would you want to type that everytime you made a declaration!? If you were to be using the datatype multiple times, you'd probably want to typedef it to be able to use one word rather than that huge amount of characters. This example is a bit exagerated, but I think it's a pretty good illustration of my point. Just because it's not necissary to use when declaring a struct for name simplification, it can be used in other places for the same exact reason.
Also, it's still extremely useful in making programs easily portable, especially concerning file-io:
Say you have an application where the number of bytes an integer takes up actually does matter. You can't be guaranteed that all compilers have a short int that's 2-bytes, or an integer as 4-byte, or a long as 4-bytes, or a compiler-defined int as 64-bytes, or or even a char as signed or unsigned.
In order to make it easy to port, you can typedef datatypes such as int32 or int64 to the associated types for that particular compiler. Then, when you go to compile it in another compiler, all you have to do is change that one file with the typedefs and you immediately are able to adjust any datatypes that must be of a certain size.
One example of why it's necissary to use datatypes of particular length -- let's say you want to read in 4 bytes from a file and store it to an integer. If the integer isn't 4 bytes, then you'll either not write to the rest of the integer (if it's over 4 bytes), or worse, you'll write past the boundaries of the integer (if it's under 4 bytes).
No -- typedef obeys scoping rules while #define does not.Originally posted by beege31337
would this function any differently ( from the programmers point of view ) than using a preprocessor definiton,
like would
typedef int int_32;
sort of work the same as
#define int_32 int
Just wondering because I have never used typedef before
You're better off using typedef in almost all cases
They are different, but I have seen them used in similar situations.[Yes, they are similar in function.
You are better off using typedef- like was mentioned, especially with linked lists.
__________________
Mr. C: Author and Instructor