Question

I'm not entirely sure what this is called, but I think it is very easy to do.

I have seen some people rename the types of variables to make their code easier to read. Let's say I have a shop of items and they need a int "itemId".

How could I define so that I can say:

Item getItem(ID itemId);

Insteath of:

Item getItem(int itemId);

I don't nessesarily know if it's any useful to always change code like that. But I would at the very least want the knowledge to know how to do it. Anyways, I'm quite sure it's almost as easy as:

#define ID as int;

or something in that manner. But I just were not able to look it up as I don't remember what the action is called x) Thanks

Was it helpful?

Solution

C++11:

using ID = int;

C++11 and previous standards:

typedef int ID;

OTHER TIPS

both #define ID int and typedef int ID can work and will have the same effect in your example; But there are differences between the two: the first defines a string literal which will be replaced with "int" at compile time, while the second defines a new data type. The later is the recommended way, as it is less error prone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top