Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-05-24 08:35:30


From: colin.rafferty_at_[hidden]
> Rob Stewart <stewart_at_[hidden]> wrote:
> > From: "Reece Dunn" <msclrhd_at_[hidden]>
> >> Rob Stewart wrote:
>
> >>> class char_string
> >>> {
> >>> virtual iterator begin() = 0;
> >>> virtual const_iterator begin() const = 0;
> >>> };
>
> >> That is what I was referring to. You need to change the name of the const
> >> version if you are to allow virtual functions.
>
> > Why?
>
> I'm not sure if this is what Reece is alluding to, but the problem is

No, it isn't. He didn't realize that virtual functions could be
overloaded.

> if you want to subclass and only overload some of the begin()
> versions.

Note that the overloaded functions are declared pure virtual.
They both have to overridden somewhere. The derived class could
be left abstract by only overriding one of the two, so that does
still leave to a further derived class the need to prevent hiding
of either of them.

> class concrete_char_string : public char_string
> {
> virtual iterator begin();
> virtual const_iterator begin() const;
> };
>
> class my_char_string : public concrete_char_string
> {
> virtual iterator begin() const;
> };
>
> my_char_string mcs = foo();
>
> mcs.begin(); // calls const version
>
> We may have expected to call concrete_char_string's non-const begin(),
> but since it has the same name as my_char_string's begin(), it is
> hidden, and we call my_char_string's const version.

Sure, but you changed the problem. Your begin() overloads in the
concrete_char_string class are no longer pure virtual.

> This is a general problem problem with having multiple virtual
> functions with the same name. It's why people will have set_foo() and
> get_foo() rather than two versions of foo().

Such functions are not likely candidates for being virtual and
are unlikely to be hidden by a redeclaration in a derived class.
The reason for such names is that some folks dislike the
overloaded use of the same name to mean assignment or query
depending upon context.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk