By any chance is it possible to do polymorphism with the same name of function with different parameters?
For example, I want those three functions to become one.
virtual bool isValid1(const std::string&) = 0;
virtual bool isValid2(const uint32_t&) = 0;
virtual bool isValid3(const int& num) = 0;
In other words, I have a Base A
and three Derived A class. I also have Base B
that holds a vector of Base A class and a T(template type, could be a string
, unit32
or int
) argument. When I want to call from Base B
the isValid
function using the vector
and argument (like this: isValid(argument)
) I am a bit stuck, since I have to know before if to call isValid1(string)
, isValid2(uint32)
or isValid3(int)
.
Some piece of the code for those who asked,
template <class T>
class Field
{
public:
void addValidator(BaseValidator* validator) { m_validator.push_back(validator); }
void validate() const { m_validator[0].isValid(m_data);}
private:
std::vector<BaseValidator*> m_validator;
T m_data;
};
- I am opting for polymorphism, yet other type of solutions are welcome.
- Please let me know if the question is too short to understand, so I can rewrite in a different method.