本文发表在 rolia.net 枫下论坛<b>Redefinition Hides Methods</b>
Suppose you create something like the following:
class Dwelling
{
public:
virtual void showperks(int a) const;
...
};
class Hovel : public Dwelling
{
{
public:
void showperks();
...
};
This causes a problem. You may get a compiler warning similar to the following:
Warning: Hovel::showperks(void) hides Dwelling::showperks(int)
Or perhaps you won't get a warning. Either way, the code has the following implications:
Hovel trump;
trump.showperks(); // valid
trump.showperks(5); // invalid
The new definition defines a showperks() that takes no arguments. Rather than resulting in two overloaded versions of the function, this redefinition hides the base class version that takes an int argument. In short, redefining inherited methods is not a variation of overloading. If you redefine a function in a derived class, it doesn't just override the base class declaration with the same function signature. Instead, it hides all base class methods of the same name, regardless of the argument signatures.
This fact of life leads to a couple of rules of thumb. First, if you redefine an inherited method, make sure you match the original prototype exactly. One exception is that a return type that is a reference or pointer to a base class can be replaced by a reference or pointer to the derived class. (This exception is new, and not all compilers recognize it yet. Also, note that this exception applies only to return values, not to arguments.) Second, if the base class declaration is overloaded, redefine all the base class versions in the derived class:
class Dwelling
{
public:
// three overloaded showperks()
virtual void showperks(int a) const;
virtual void showperks(double x) const;
virtual void showperks() const;
...
};
class Hovel : public Dwelling
{
public:
// three redefined showperks()
void showperks(int a) const;
void showperks(double x) const;
void showperks() const;
...
};
If you redefine just one version, the other two become hidden and cannot be used by objects of the derived class. Note that if no change is needed, the redefinition can simply call the base-class version.
Would you say <i>thank you</i> to me ?
;-)
BTW, I am not good at programming. Just curious to the question.
and know how to find answers.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Suppose you create something like the following:
class Dwelling
{
public:
virtual void showperks(int a) const;
...
};
class Hovel : public Dwelling
{
{
public:
void showperks();
...
};
This causes a problem. You may get a compiler warning similar to the following:
Warning: Hovel::showperks(void) hides Dwelling::showperks(int)
Or perhaps you won't get a warning. Either way, the code has the following implications:
Hovel trump;
trump.showperks(); // valid
trump.showperks(5); // invalid
The new definition defines a showperks() that takes no arguments. Rather than resulting in two overloaded versions of the function, this redefinition hides the base class version that takes an int argument. In short, redefining inherited methods is not a variation of overloading. If you redefine a function in a derived class, it doesn't just override the base class declaration with the same function signature. Instead, it hides all base class methods of the same name, regardless of the argument signatures.
This fact of life leads to a couple of rules of thumb. First, if you redefine an inherited method, make sure you match the original prototype exactly. One exception is that a return type that is a reference or pointer to a base class can be replaced by a reference or pointer to the derived class. (This exception is new, and not all compilers recognize it yet. Also, note that this exception applies only to return values, not to arguments.) Second, if the base class declaration is overloaded, redefine all the base class versions in the derived class:
class Dwelling
{
public:
// three overloaded showperks()
virtual void showperks(int a) const;
virtual void showperks(double x) const;
virtual void showperks() const;
...
};
class Hovel : public Dwelling
{
public:
// three redefined showperks()
void showperks(int a) const;
void showperks(double x) const;
void showperks() const;
...
};
If you redefine just one version, the other two become hidden and cannot be used by objects of the derived class. Note that if no change is needed, the redefinition can simply call the base-class version.
Would you say <i>thank you</i> to me ?
;-)
BTW, I am not good at programming. Just curious to the question.
and know how to find answers.更多精彩文章及讨论,请光临枫下论坛 rolia.net