I have a class with a private data member of type vector< A*>
.
The class has two public methods that actually use vector<A*>::size_type
:
I can add to public section of the class the following typedef:
typedef vector::size_type SIZE_t;
but IMHO it exposes too many details about class implementation.
Another approach is to use size_t
.
What do you think?
I would use a typedef in the class. The reason is that for std::vector
, the size type is std::size_t
, but if you later change the code to use a container (hand rolled) whose size type is not std::size_t
redefining the typedef will be enough.
Using that typedef does not expose any detail of implementation, and it in fact helps encapsulate. The important element in the typedef is the local name, not what it is defined to be.
for ( mytype::size_type i = 0; i < myelement.size(); ++i )
In the for loop above, user code is unaware of whether size_type
is a signed or unsigned type, it just works. You can change your implementation, and as long as you update the typedef the previous code will compile without signed/unsigned comparison warnings. The typedef actually helps encapsulation.
Use plain old size_t
for both member functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With