class NumericInput : public TextInput
{
public:
TextInput input;
This is almost definitely not what you want. If you're inheriting, I can't think of a good reason you'd ever also want composition. Get rid of this input
member variable. When you inherit, you already get everything from the parent class (including value
in this case).
To specifically call the parent class's add()
, you can do TextInput::add()
, so your child add could look like:
void add(char c)
{
if (isdigit(c))
{
TextInput::add(c); // Call parent add()
}
}
Furthermore, if you want to call add()
on a TextInput
pointer that's pointing to a NumericInput
, and you want it to resolve to call the NumericInput
's add()
, you must declare the base add()
to be virtual
:
virtual void add(char c) { // ...
Lastly, you can still append to a string even if there's nothing in it, so your check in the base add()
does nothing.
Altogether, that'll make your code look like:
class TextInput
{
public:
std::string value;
virtual void add(char c) { value += c; }
std::string getValue() { return value; }
};
class NumericInput : public TextInput
{
public:
void add(char c)
{
if (isdigit(c)) { TextInput::add(c); }
}
// You already get this for free from the inheritance
// std::string getValue()
// {
// return value;
// }
};
Live example here: https://ideone.com/kQxAPo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…