Boost logo

Boost :

From: colin.rafferty_at_[hidden]
Date: 2004-05-21 15:00:05


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
if you want to subclass and only overload some of the begin()
versions.

    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.

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().

-- 
Colin Rafferty

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