I'm not sure what you're talking about.
If you want to override a base class function in a derived class, you just need to declare the function in the base class as
virtual
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
class Base
{
public:
virtual void foo();
};
void Base::foo()
{
// Blah blah...
}
class Derived: public Base
{
public:
void foo(); // Overrides Base::foo
};
void Derived::foo()
{
// Blah blah...
// If you want to call the base class's 'foo' function, use this:
Base::foo();
}
|
(You should probably make the destructors virtual, too.)
If you're trying to make it so you don't have to declare the function at all (as in, so that you don't have to type
void foo();
again) in the derived class...I'm not sure why you would want to do that. Where would the code for the function go?
What C++11 added was this:
1 2 3 4 5
|
class Derived: public Base
{
public:
void foo() override; // The 'override' tells the compiler that this overrides 'Base::foo'
};
|
In our case, this is exactly the same as above, though it conveys your intentions more clearly and also helps you catch an error in case your declaration of
Derived::foo doesn't actually override the one in the base class (for instance, if the parameters are different or something).
C++11 also added this:
1 2 3 4 5 6 7 8 9 10
|
class Derived: public Base
{
public:
void foo() final; // The 'final' tells the compiler that you can't override this function
};
class MoreDerived: public Derived
{
public:
void foo() override; // Error: overriding a 'final' function
};
|
I think that might be what you're referring to when you say "disable the redeclaration".
900!