what's the difference?
--TING
what's the difference?
--TING
Cheap and easy answer: #define exists, #typedef does not.
Real answer: typedef gives a new name to an existing type, for one reason or another. For example, if you look at cstdint (the header file, or stdint.h), you'll see a bunch of typedef's. The reason being, sometimes people need to know that they're using, say, a 32-bit integer; they can declare their variable as "int_32t" and know that the compiler has typedef'ed that to whatever the appropriate type is (int, or long, or whatever).
#define is just search-and-replace. Every appearance of the first token is replaced by whatever comes after it. It can do more than just types, but macros and constants. Common usage (in C, not necessarily C++) would be something like
to generate a constant.Code:#define MAX_SIZE 80
#define - is substitution of the string performed by preprocessor
typedef - alias of the type done by compiler
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler