문제

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

도움이 되었습니까?

해결책

C++11:

using ID = int;

C++11 and previous standards:

typedef int ID;

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top