One way to add methods to a class without changing it is with inheritance (e.g., Add a method to existing C++ class in other file). However, this solution causes a problem to the toy program below:
#include <vector>
class c_A {
public:
std::vector<int> v;
c_A one_element(void) {
c_A res;
res.v = v;
res.v.resize(1);
return res;
};
};
class c_Aext : public c_A {
// Methods here
};
int main () {
c_Aext Aext;
Aext.v = {0, 1, 2};
c_Aext B = Aext.one_element(); // The problem
return 0;
};
What are the possible solutions to this problem? Casting? Removing the class c_Aext and integrating its methods in C_A? ...