So, you can't create such a function, I know. However, I really need. In this regard, I wonder if there are any workarounds? So far, I have not thought of anything like that:
class A { protected: virtual void _func(const char*) = 0; // метод для класса B virtual void _func(float) = 0; // метод для класса C public: template<class T> void func(T e) { this->_func(e); } }; class B : public A { private: void _func(const char* i) {}; protected: void _func(float i) { std::cout << i << std::endl; }; }; class C : public A { private: void _func(float i) { std::cout << "тест" << std::endl; }; protected: void _func(const char* i) { std::cout << i << std::endl; }; }; int main() { B b; b.func(14.4); // вызовем _func(float) из B C c; c.func("Привет"); // вызовем _func(int) из C c.func(14.9); return 0; } However, the drawbacks are obvious: you have to redefine an unnecessary virtual function in every class that you don’t need to use. In this case, you can pass an argument to each child class that we don’t want to pass (shown in the void _func (float i) method {std :: cout << "test" << std :: endl;}; ) Are there any more elegant solutions? ? Thank.
Aas a class capable of transmitting bothchar*andfloat, and if any of its descendants ISA, then it must also be able to. Because he isASo either descendants should be able to do everything that an ancestor can do, or the hierarchy itself needs to be changed - for the design itself is incorrect ... - Harry