Archived

This topic is now archived and is closed to further replies.

Lalle

typedef questions

Recommended Posts

Lalle    122
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

Share this post


Link to post
Share on other sites
Dactylos    122
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

Share this post


Link to post
Share on other sites
abdulla    164
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.

Share this post


Link to post
Share on other sites
Arild Fines    968
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"

Share this post


Link to post
Share on other sites
Dactylos    122
It''s not exactly required, but it does simplify some thing.

e.g. without the typedef:
  
// declare a struct

struct 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.

Share this post


Link to post
Share on other sites
Neophyte    595
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

Share this post


Link to post
Share on other sites
jwalker    122
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 !! }

Share this post


Link to post
Share on other sites