Scenario:
foo.h:
#include <vector>
class foo {
public:
std::vector<int>* getVector();
/* ... other methods declarations ... */
}
foo.cpp:
#include "foo.h"
#include <vector>
/* ... other methods definitions using std::vector ... */
std::vector<int>* foo::getVector() {
return new std::vector<int>();
}
I want .cpp to be independent of any possible future changes in the header. If for whatever reason the interface of the class changes and the dependency from <vector>
can be eliminated, I risk that other methods in the .cpp also lose that inclusion.
Is it correct to repeat the inclusion of <vector>
in both the .cpp and .h? Does this practice make sense or should I just rely on the inclusions made in the header?
std::vector<int> v;
– CloudberrygetVector
was a getter. That would be unnecessary usage of pointers. With the non-misread code, it depends how you're using it. – Cloudberry