joaocandre
joaocandre

Reputation: 1745

Is it possible to having analogous members in different classes with different return types?

If one defines a class multiA holding a vector of instances of class A, is there a way to have multiA "inherit" (for lack of a better term) all the public member functions of A, returning a std::vector with the output?

#include <vector>

class A {
 public:
    A();
    float member1();
    float member2();
    // ...
};

class multiA {
 public:
    multiA();

    // ...
    //  std::vector<float> member1();

 protected:
    std::vector<A> data;
};

I understand I can just define an analogous member function in multiA, but would require me change it if any member of A is rewritten, and a more general approach would maybe allow me to use multiA as template to several classes? I am wondering if there is way to "automatically" create members for multiA based on A and change their return type.

Upvotes: 0

Views: 44

Answers (1)

nnolte
nnolte

Reputation: 1780

That is not possible with plain c++. Maybe if you employ some heavy macro magic one would be able to automate this for the types for which it is even possible, but the effort you need to put in this will almost never be worth it.
Note that you would first need to define all operations possible on A to also implement on std::vector<A>, which is already pretty hard to automate.

Upvotes: 1

Related Questions