typedef questions

Started by
6 comments, last by Lalle 22 years, 6 months ago
I learned C++, without learning C before. In my C++ book it states that typedef is used to make an alias for a code fragment: typedef unsigned short int USHORT; The problem is when I see a tutorial in C using typedef, it someimes looks more like a class decleration than anything else. typedef struct CAR { int a; int b; } CAR_t //this syntax may be completely wrong, but I think it looks //kind of like that So my question is.. How and why are typedef used to make theese class things? Is this the C way of writing classes(or structs)? If this is a type of class, what are the differences between typedef and a regular class? Thanks Lalle
Advertisement
typedef is simply a way of creating a named alias for another type.

The syntax is basically:

typedef TYPE new_type_alias

Where TYPE can be any type, such as a struct, so you see, your example is simply a declaration of a struct (CAR), and an alias for that struct (CAR_t)
typedef struct CAR{int a;int b;} CAR_t   
The green stuff is the 'TYPE' in the declaration above, and CAR_t is the 'new_type_alias'.

The whole thing could just as well have been written:

struct CAR{int a;int b;}; typedef CAR CAR_t; 


Edited by - Dactylos on October 15, 2001 9:15:32 AM
it''s useful to mask implemetaion things, also its useful for longnames, have a look in the STL (eg the typedef for stf::string is std::basic_string, or the typedef or size_type). I still think the best C++ book even for beginners is stroustrup''s, it really dive''s into it.

PS you dont need typedefs, they just help to make some things easier.
C requires those typedefs for struct declarations. Otherwise if you wanted an instance of the struct you''d have to declare it like this:
  struct Bleh{   int blagh;};struct Bleh myBleh;  



"A society without religion is like a crazed psychopath without a loaded .45"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
It''s not exactly required, but it does simplify some thing.

e.g. without the typedef:
  // declare a structstruct Point{   int x, y;};  int main(void){   // create a variable of type Point:   struct Point p; // the struct is necessary here}  

Since Point is not a typename you have to use the whole ''qualifier'' (struct Point) when declaring a variable of struct-type.

With typedef:
  typedef struct Point{   int x, y;} Point;  int main(void){   // create a variable of type Point:   Point p;}  

now Point is a typename (declared by the typedef), so there is no need to write "struct Point" when declaring variables.
Lalle: You''re right. That is the C-way of declaring structs. The reason for this is that in C struct-names reside in their own namespace. Which means that in general "CAR" and "struct CAR" is two different things. In C you would have to write "struct CAR" everytime you''re talking of the struct of type CAR, which can get somewhat annoying, so it is usual to typedef "struct CAR" to "CAR".
This is not necessary in C++, however it still works.

-Neophyte
>>I still think the best C++ book even for beginners is >>stroustrup''s

You gotta be kidding?
typedefs are real built types.

  typedef vector<OBJECT> object_vector;  


and also useful for dynamic types

  template <class T>class A{public:  typedef T DATA;  typedef T * LPDATA;}  


{ Stating the obvious never helped any situation !! }

This topic is closed to new replies.

Advertisement