Answer the question
In order to leave comments, you need to log in
Should I use typedef?
I am only interested in the usefulness of using typedef in terms of further code support. Is it worth writing
typedef int mytype;
mytype newVariable;
int newVariable;
Answer the question
In order to leave comments, you need to log in
use by default. it's better
if only because today it's an int, tomorrow it can be a double, and the day after tomorrow si::minutes
the main thing is that it's a minute, and not how it is implemented, right?
typedef int minutes;
minutes x;
int x;
I use typedef only for shorthand.
For example:
typedef std::pair <std::vector <somens::sometype>, anotherns::antohertype> myPair;
Without a typedef, you most likely won't be able to write a portable application.
On the one hand, the name of the new type will tell the developer about its purpose, it will be clear that the minutes type is minutes, not hours or anything else. On the other hand, information about the parent type is hidden from the developer: range of values, format, etc.
should be used, if only because:
minutes TravelTime;
much better read than
int TravelTime;
Everything depends on the specific case. Your question is too abstract.
This code says absolutely nothing about what mytype is and how it will be used.
The same is true for the "minutes" type. Without understanding how and where it will be used, it is impossible to answer. In the general case, to work with time, it is more convenient to use special classes that can represent time not only in minutes, but also in months-days-hours.
As for me, it makes sense to use typedef in such cases:
1. Shorten long types (in C ++ 0x you can use auto for this)
2. For a possible choice between precision and speed / used_memory (between float and double, for example)
typedef int mytype;
mytype newVariable;
Well, for code portability between architectures with different bitness, there are already types size_t, uintptr_t, ptrdiff_t. No need to reinvent the wheel.
>should be used, if only because:
>minutes TravelTime;
>much better readable than
>int TravelTime;
This is from the evil one. Behind the imaginary readability, it is not at all clear whether I can write
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question