operator inheritance problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Juha Tenhosaari

    operator inheritance problem

    Hello,

    I wonder if anyone could tell me why the operator= is not inherited into the
    derived class in the following code:

    class A
    {
    public:
    void operator=(int i){a=i;}
    int a;
    }

    class B : public A
    {
    public:
    int b;
    }

    void main(void)
    {
    B bb;
    bb=3; // trying to use the overloaded operator
    cout << bb.a;
    }

    When I try to compile this, I get the error message "could not find match
    for B::operator=(in t)". If I replace the overloaded operator by an ordinary
    function (i.e. operator=(int) => set(int)), it works fine.

    What am I doing wrong?


    Juha


  • John Carson

    #2
    Re: operator inheritance problem

    "Juha Tenhosaari" <j.t@invalid.co m> wrote in message
    news:dPdkb.2997 4$mU6.78385@new sb.telia.net[color=blue]
    > Hello,
    >
    > I wonder if anyone could tell me why the operator= is not inherited
    > into the derived class in the following code:
    >
    > class A
    > {
    > public:
    > void operator=(int i){a=i;}
    > int a;
    > }
    >
    > class B : public A
    > {
    > public:
    > int b;
    > }
    >
    > void main(void)
    > {
    > B bb;
    > bb=3; // trying to use the overloaded operator
    > cout << bb.a;
    > }
    >
    > When I try to compile this, I get the error message "could not find
    > match for B::operator=(in t)". If I replace the overloaded operator by
    > an ordinary function (i.e. operator=(int) => set(int)), it works fine.
    >
    > What am I doing wrong?
    >
    >
    > Juha[/color]


    Simple. Assignment operators are not inherited --- just like constructors
    are not inherited.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)

    Comment

    • Tim Threlfall

      #3
      Re: operator inheritance problem

      13.5.3 of the standard:

      Because a copy assignment operator operator= is implicitly declared for
      a class if not declared by the user (12.8), a base class assignment
      operator is always hidden by the copy assignment operator of the
      derived class.

      Comment

      • grejdanospam@pacbell.net

        #4
        Re: operator inheritance problem

        On Sun, 19 Oct 2003 02:36:44 +1000, John Carson
        <donaldquixote@ datafast.net.au > wrote:
        [color=blue]
        > "Juha Tenhosaari" <j.t@invalid.co m> wrote in message
        > news:dPdkb.2997 4$mU6.78385@new sb.telia.net[color=green]
        >> Hello,
        >>
        >> I wonder if anyone could tell me why the operator= is not inherited
        >> into the derived class in the following code:
        >>
        >> class A
        >> {
        >> public:
        >> void operator=(int i){a=i;}
        >> int a;
        >> }
        >>
        >> class B : public A
        >> {
        >> public:
        >> int b;
        >> }
        >>
        >> void main(void)
        >> {
        >> B bb;
        >> bb=3; // trying to use the overloaded operator
        >> cout << bb.a;
        >> }
        >>
        >> When I try to compile this, I get the error message "could not find
        >> match for B::operator=(in t)". If I replace the overloaded operator by
        >> an ordinary function (i.e. operator=(int) => set(int)), it works fine.
        >>
        >> What am I doing wrong?
        >>
        >>
        >> Juha[/color]
        >
        >
        > Simple. Assignment operators are not inherited --- just like constructors
        > are not inherited.
        >
        >[/color]
        They are inherited but hiddem , see response from Tim.

        Adding using declaration to class B :

        using A::operator =(int);

        will bring it back to the public/protected/private view.

        --
        grzegorz

        Comment

        • John Carson

          #5
          Re: operator inheritance problem

          <grejdanospam@p acbell.net> wrote in message
          news:oprw8z44uk 6u0rcr@news.sf. sbcglobal.net[color=blue]
          > On Sun, 19 Oct 2003 02:36:44 +1000, John Carson
          > <donaldquixote@ datafast.net.au > wrote:
          >[color=green]
          > > "Juha Tenhosaari" <j.t@invalid.co m> wrote in message
          > > news:dPdkb.2997 4$mU6.78385@new sb.telia.net[color=darkred]
          > > > Hello,
          > > >
          > > > I wonder if anyone could tell me why the operator= is not
          > > > inherited into the derived class in the following code:
          > > >
          > > > class A
          > > > {
          > > > public:
          > > > void operator=(int i){a=i;}
          > > > int a;
          > > > }
          > > >
          > > > class B : public A
          > > > {
          > > > public:
          > > > int b;
          > > > }
          > > >
          > > > void main(void)
          > > > {
          > > > B bb;
          > > > bb=3; // trying to use the overloaded operator
          > > > cout << bb.a;
          > > > }
          > > >
          > > > When I try to compile this, I get the error message "could not
          > > > find match for B::operator=(in t)". If I replace the overloaded
          > > > operator by an ordinary function (i.e. operator=(int) =>
          > > > set(int)), it works fine.
          > > >
          > > > What am I doing wrong?
          > > >
          > > >
          > > > Juha[/color]
          > >
          > >
          > > Simple. Assignment operators are not inherited --- just like
          > > constructors are not inherited.
          > >
          > >[/color]
          > They are inherited but hiddem , see response from Tim.
          >
          > Adding using declaration to class B :
          >
          > using A::operator =(int);
          >
          > will bring it back to the public/protected/private view.
          >
          > --
          > grzegorz[/color]



          I think this satisfies my definition of "not inherited" --- and apparently
          Stroustrup's too (see TC++PL, p. 307). Nevertheless, the technique for
          making it accessible to the derived class is worth pointing out. However,
          the using declaration should not specify the operator's argument. It should
          be:

          using A::operator = ;

          Overload resolution will then occur in the usual way.


          --
          John Carson
          1. To reply to email address, remove donald
          2. Don't reply to email address (post here instead)

          Comment

          • Juha Tenhosaari

            #6
            Vs: operator inheritance problem


            <grejdanospam@p acbell.net> wrote:[color=blue][color=green]
            > > Adding using declaration to class B :
            > >
            > > using A::operator =(int);
            > >
            > > will bring it back to the public/protected/private view.[/color][/color]

            John Carson wrote:[color=blue]
            > the using declaration should not specify the operator's argument. It[/color]
            should[color=blue]
            > be:
            >
            > using A::operator = ;[/color]

            Thanks for the answers.

            I modified the derived class this way:

            class B : public A
            {
            using A::operator=;
            public:
            int b;
            }


            ....but my compiler gave me error message "Identifier expected". I wonder
            why.

            This, however, did work:

            class B : public A
            {
            public:
            void operator=(int i){A::operator= (i);}
            int b;
            }


            Juha


            Comment

            • John Carson

              #7
              Re: operator inheritance problem

              "Juha Tenhosaari" <juha.news@teli a.com> wrote in message
              news:qWukb.3379 7$dP1.106935@ne wsc.telia.net[color=blue]
              >
              > Thanks for the answers.
              >
              > I modified the derived class this way:
              >
              > class B : public A
              > {
              > using A::operator=;
              > public:
              > int b;
              > }
              >
              >
              > ...but my compiler gave me error message "Identifier expected". I
              > wonder why.[/color]


              You don't say the line at which this error message occurs. I believe that by
              putting the using declaration at the start of the class, you have implicitly
              made the operator private. Try moving it after public:
              Note that compiler implementation of using declarations are very buggy, so I
              would not be surprised if it didn't work.

              After moving the using declaration to after public:, the code compiles find
              using Dinkum Exam.




              --
              John Carson
              1. To reply to email address, remove donald
              2. Don't reply to email address (post here instead)

              Comment

              Working...