If your class is already using std::vector<A*>
in its implementation, then adding typedef std::vector<A*>::size_type size_type
does not expose any more details than it already exposes.
However, if you are going for full encapsulation, you would want to a technique such as the PIMPL idom or an interface class (also known as protocol class), completely hiding that std::vector<A*>
is used in the implementation at all:
Before:
#include <vector>
class A;
class Foo {
public:
typedef std::vector<A*>::size_type size_type;
size_type get_number_of_stuff() const;
private:
std::vector<A*> _stuff;
};
After (using PIMPL technique):
class FooImpl;
class Foo {
public:
typedef size_t size_type; // or just use size_t directly below
size_type get_number_of_stuff() const;
private:
FooImpl* _impl;
};
FooImpl is defined in your source file, not the header, completely hiding the choice of vector in the implementation details. Therefore, you no longer need to #include <vector>
in your header file anymore, which has a couple benefits:
- Users of your header file don't have to (indirectly) include vector if they don't use it. This can improve compile-time performance, which is important in larger code bases.
- If you change the implementation (e.g., to list), you don't risk breaking any code that (erroneously) relied on your
#include <vector>
, which is now #include <list>
. It happens.