typedef

what is the use of typedef if i can actually just use a type?
also i mean typedef is just for making an alias for a type?
Typedef is a keyword used to define a new type.
Int, char are primitive predefined types, and you create your own new type .

Samples can be found here: http://www.cplusplus.com/doc/tutorial/other_data_types/
Last edited on
Just like making an alias of an existing type.

eg:
1
2
3
4
5
typedef std::vector<std::string> vecOfStr; //now the alias has been created
vecOfStr strings;   //this is now the same as std::vector<std::string> strings;

strings.push_back("I was created with a typedef.");
//............. 


Aceix.
only an existing type can have an alias?
Give me an example of a type that doesn't exist.

Aceix.
If you use a typedef it's easier to change the type, because you only need to change the code in one position.

The size of primitive types (short, int, long, etc.) depends on the compiler and platform you're on. If you want a 32 bit unsigned integer you could use unsigned int and it might work for you but it might not work when compiled with a different compiler or when compiling for a different platform. In this case it's much better to use a typedef. Then you can write code that will work almost everywhere and all you need to change is the typedef. Fixed size integers are so useful that the C++ standard library provides a number of typedefs for you in the <cstdint> header.
http://www.cplusplus.com/reference/cstdint/

Typedefs can also be used to make type(def)s shorter. Maybe you use std::vector<int>::size_type in your code. It can look a bit bloated if you have to write it over and over again. You could use a typedef typedef std::vector<int>::size_type VecIntSize; and then you could just write VecIntSize instead.

DevTK wrote:
Typedef is a keyword used to define a new type.

It doesn't create a new type. A typedef is just an alias.
Let's say you create a data structure (e.g a linked list) and you want to store integers in your data structure.

That's fine, until you want to use your data structure again. Except this time you want to store doubles.

Rather than having to go through your code and change every instance of int to double, you could at the beginning say,

typedef int value_type;

Meaning, when you want to use a different data type, the only line of code you need to change is that one to,

typedef double value_type;

I hope that was clear, I'm not the best at explaining things haha.

@Aceix
Give me an example of a type that doesn't exist.

Aceix
@Peter87

The size of primitive types (short, int, long, etc.) depends on the compiler and platform you're on. If you want a 32 bit unsigned integer you could use unsigned int and it might work for you but it might not work when compiled with a different compiler or when compiling for a different platform


nice i can see it way clearer
sec im going to review it again
Last edited on
@MrPigeon

yeah that was clear :) it helps me alot since im having trouble with this typedef
Topic archived. No new replies allowed.