S
Steve Folly
Hi,
I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:
class Base
{
public:
virtual void Foo() const;
}
class Derived : public Base
{
public:
virtual void Foo() const;
}
If I want to change Foo() to a non-const method I'll have to find all the
places where Foo() is overridden and change those as well. A daunting task
in a big project! And quite possible that some may be missed.
The ones I missed would of course be found eventually by testing, but if I
could catch them at compile time, so much the better.
Now, I haven't seen this in any FAQs, but if I've missed one that describes
my solution then I swear it's coincidence!
If I changed Base::Foo to be non-const, but left Derived::Foo const, the
compiler would not warn me. Fair enough. But, if I make Base:
class Base
{
public:
virtual void Foo();
class NotOverridable;
virtual NotOverridable* Foo() const { return 0; }
}
The compiler is nice enough to tell me the return type differs and is not
co-variant. Of course, NotOverridable should not be defined, and the method
still needs a body, otherwise a link error occurs because it's virtual. (Is
there any way around this?)
Does this seem like a good idea? It seems to work for my case, but is there
likely to be anything I've missed that might make this bad on a wider scale?
--
Regards,
Steve
"...which means he created the heaven and the earth... in the DARK! How good
is that?"
I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:
class Base
{
public:
virtual void Foo() const;
}
class Derived : public Base
{
public:
virtual void Foo() const;
}
If I want to change Foo() to a non-const method I'll have to find all the
places where Foo() is overridden and change those as well. A daunting task
in a big project! And quite possible that some may be missed.
The ones I missed would of course be found eventually by testing, but if I
could catch them at compile time, so much the better.
Now, I haven't seen this in any FAQs, but if I've missed one that describes
my solution then I swear it's coincidence!
If I changed Base::Foo to be non-const, but left Derived::Foo const, the
compiler would not warn me. Fair enough. But, if I make Base:
class Base
{
public:
virtual void Foo();
class NotOverridable;
virtual NotOverridable* Foo() const { return 0; }
}
The compiler is nice enough to tell me the return type differs and is not
co-variant. Of course, NotOverridable should not be defined, and the method
still needs a body, otherwise a link error occurs because it's virtual. (Is
there any way around this?)
Does this seem like a good idea? It seems to work for my case, but is there
likely to be anything I've missed that might make this bad on a wider scale?
--
Regards,
Steve
"...which means he created the heaven and the earth... in the DARK! How good
is that?"