How to invoke the operator of the private base class?

Discussion in 'C++' started by PengYu.UT, Apr 1, 2006.

  1. PengYu.UT

    PengYu.UT Guest

    I try to invoke the [] operator of the base class.

    ** line doesn't work. But *** line works. However, *** lines becomes
    problematic with multiple inheritance is used for "derived". ****
    works, but it is a little bit cumbersome. I'm wondering if there any
    way to make ** works?


    #include <iostream>
    #include <cstring>

    class base {
    public:
    int operator[](int i) {
    return i;
    }
    int get(int i) {
    return i;
    }
    };

    class derived : private base {
    public:
    derived(int i) {
    std::cout << "base: " << base << std::endl; //error
    **
    std::cout << "base: " << (*this) << std::endl; //no error
    ***
    std::cout << "base: " << base::eek:perator[](i) << std::endl;//
    no error ****
    std::cout << "base.get(i): " << base::get(i) << std::endl;
    }
    };

    int main(int argc, char *argv[])
    {
    derived a(10);
    }
     
    PengYu.UT, Apr 1, 2006
    #1
    1. Advertisements

  2. The syntax "[blah]" is only applicable to _expressions_, and further
    only to those that evaluate to objects of classes that overload the
    operator[] or to pointers.

    The syntax "[blah]" is not applicable to _types_. "base" is a *type*.
    Of course. "(*this)" is an *object* of type that overloads op[] (by
    inheriting the base's one).
    What do you mean by that? You can always static_cast your 'this' to
    avoid ambiguity.



    V
     
    Victor Bazarov, Apr 1, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.