lundi 30 août 2021

How to implement pb message polymorphism like c++

In C++, I have a class T, it's constructed like:

class T {
public:
    T(X* x) { // X is pb message 
     .....
    }
    // some func use x
    .....
};

Then, a class A which uses T like:

class A {
    void funcA(X* x) { // X is pb message 
        T* t = new T(x);
        ....
    }
};

And now, a new class B wants to use T, but this class use message Y. Message Y and X are similar. But it was already used by other service, so I can't just change it to X. (So does X.)

class B {
    void funcB(Y* y) { // y is pb message, which is similar with x. 
        T* t = new T(y); // error
        ....
    }
};

Copy T and make a new class T1 can solve this problem, but it's weird, not a good design.

class T1 {
public:
    T1(Y* y) { // y is pb message 
     .....
    }
    // some func use y
    .....
};

If X, Ys were common C++ class, I could make them inherit from Z, and change T(X* x); to T(Z* z); But they are both pb message, is there any solution? Maybe change T constructor to a template func?

Please help me. Thank you!

Aucun commentaire:

Enregistrer un commentaire