How to invoke the operator of the private base class?

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

  1. 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);
    }
     
    , Apr 1, 2006
    #1
    1. Advertising

  2. wrote:
    > I try to invoke the [] operator of the base class.
    >
    > ** line doesn't work.


    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*.

    > But *** line works.


    Of course. "(*this)" is an *object* of type that overloads op[] (by
    inheriting the base's one).

    > However, *** lines becomes
    > problematic with multiple inheritance is used for "derived".


    What do you mean by that? You can always static_cast your 'this' to
    avoid ambiguity.

    > ****
    > works, but it is a little bit cumbersome. I'm wondering if there any
    > way to make ** works?


    No.

    > #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);
    > }



    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Apr 1, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison
    Replies:
    0
    Views:
    509
    John Harrison
    Aug 6, 2003
  2. DaveLessnau
    Replies:
    3
    Views:
    450
    Howard
    May 16, 2005
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    573
    John Carson
    Sep 3, 2005
  4. Replies:
    2
    Views:
    858
    Noah Roberts
    Jun 9, 2006
  5. Replies:
    4
    Views:
    496
    Zeppe
    Sep 12, 2008
Loading...

Share This Page