overloading ->

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ranjit mario noronha

    overloading ->


    Hi,

    I am trying to overload the ->operator as follows :


    class foo {
    public:
    int a;
    int operator->() ;

    };
    int foo::operator->() {

    cout<<"Hullo"<< endl;
    return 1l;
    }
    void main() {
    foo *f=new foo();
    cout<<f->a;
    }

    However the value of a is printed (0) and the operator function is not
    called. Does anybody know what mistake I'm making ?

    Thanks,
    __

    Ranjit

    _______________ _______________ _______________ _______________ _______________ _____

    Ranjit Noronha
    Graduate Research Associate
    Dept. of Computer and Information Sciences
    The Ohio State University
    Phone: (614)477-9900
    E-mail: noronha@cis.ohi o-state.edu

    _______________ _______________ _______________ _______________ _______________ _____


  • Bernd Jochims

    #2
    Re: overloading -&gt;


    "ranjit mario noronha" <noronha@xi.cis .ohio-state.edu> wrote in message
    news:Pine.GSO.4 .40.03090915253 60.271-100000@xi.cis.o hio-state.edu...[color=blue]
    >
    > Hi,
    >
    > I am trying to overload the ->operator as follows :
    >
    >
    > class foo {
    > public:
    > int a;
    > int operator->() ;
    >
    > };
    > int foo::operator->() {
    >
    > cout<<"Hullo"<< endl;
    > return 1l;
    > }
    > void main() {
    > foo *f=new foo();
    > cout<<f->a;
    > }
    >
    > However the value of a is printed (0) and the operator function is not
    > called. Does anybody know what mistake I'm making ?
    >
    > Thanks,
    > __
    >
    > Ranjit
    >
    >[/color]
    _______________ _______________ _______________ _______________ _______________ _
    ____[color=blue]
    >
    > Ranjit Noronha
    > Graduate Research Associate
    > Dept. of Computer and Information Sciences
    > The Ohio State University
    > Phone: (614)477-9900
    > E-mail: noronha@cis.ohi o-state.edu
    >
    >[/color]
    _______________ _______________ _______________ _______________ _______________ _
    ____[color=blue]
    >
    >///[/color]
    try changing
    foo *f=new foo();
    to
    foo* f= new foo;

    also, void main should be int main.
    Bernie


    Comment

    • Kevin Goodsell

      #3
      Re: overloading -&gt;

      ranjit mario noronha wrote:
      [color=blue]
      > Hi,
      >
      > I am trying to overload the ->operator as follows :
      >
      >
      > class foo {
      > public:
      > int a;
      > int operator->() ;[/color]

      operator-> can't return an int. It has to return something that '->' can
      be applied to (usually a pointer).
      [color=blue]
      >
      > };
      > int foo::operator->() {
      >
      > cout<<"Hullo"<< endl;
      > return 1l;
      > }
      > void main() {[/color]

      In C++, main returns int. void is not and never has been acceptable.
      [color=blue]
      > foo *f=new foo();
      > cout<<f->a;[/color]

      f->a is basically equivalent to this:

      (f.operator->())->a

      This is why operator-> cannot return an int, and must return something
      that -> can be applied to. Read about operator-> in your C++ book. It's
      not like other operators.

      Also, since you did not initialize the foo you allocated, nearly any use
      of it (other than giving it a value) invokes undefined behavior. You
      can't "use" a variable that has never been assigned a value.

      And you forgot to delete f.
      [color=blue]
      > }
      >
      > However the value of a is printed (0) and the operator function is not
      > called. Does anybody know what mistake I'm making ?
      >
      > Thanks,
      > __
      >
      > Ranjit
      >
      > _______________ _______________ _______________ _______________ _______________ _____
      >
      > Ranjit Noronha
      > Graduate Research Associate
      > Dept. of Computer and Information Sciences
      > The Ohio State University
      > Phone: (614)477-9900
      > E-mail: noronha@cis.ohi o-state.edu
      >
      > _______________ _______________ _______________ _______________ _______________ _____
      >
      >[/color]

      Think you could trim that monster down a bit? A sig with more than about
      4 or 5 lines is usually considered excessive. Wouldn't hurt if you used
      a correct sig marker, either ("-- " is used to indicate the start of a
      signature).

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      • Ron Natalie

        #4
        Re: overloading -&gt;

        ranjit mario noronha wrote:
        [color=blue]
        > I am trying to overload the ->operator as follows :[/color]

        You've got a lot of problems here.

        [color=blue]
        > int operator->() ;[/color]

        This isn't legal. Operator-> must return something that is legal to apply
        another operator -> to. An int does not meet these requirements.

        Essentially, when operator-> is overloaded f->a gets interpretted
        as (f.operator->())->a
        [color=blue]
        > void main() {[/color]

        main must return int.
        [color=blue]
        > foo *f=new foo();
        > cout<<f->a;[/color]

        This doesn't even attempt to call foo::operator->(). You invoked the operator
        on a foo*. You can't overload on pointers (only classes and enums). If you
        had written:
        foo f;
        cout << f->a;
        things might have been different (of course, with your operator-> it still won't
        compile).



        Comment

        • Bernd Jochims

          #5
          Re: overloading -&gt;


          "Bernd Jochims" <bjochims@telus .net> wrote in message
          news:bjlblf$k5a os$1@ID-51131.news.uni-berlin.de...[color=blue]
          >
          > "ranjit mario noronha" <noronha@xi.cis .ohio-state.edu> wrote in message
          > news:Pine.GSO.4 .40.03090915253 60.271-100000@xi.cis.o hio-state.edu...[color=green]
          > >
          > > Hi,
          > >
          > > I am trying to overload the ->operator as follows :
          > >
          > >
          > > class foo {
          > > public:
          > > int a;
          > > int operator->() ;
          > >
          > > };
          > > int foo::operator->() {
          > >
          > > cout<<"Hullo"<< endl;
          > > return 1l;
          > > }
          > > void main() {
          > > foo *f=new foo();
          > > cout<<f->a;
          > > }
          > >
          > > However the value of a is printed (0) and the operator function is not
          > > called. Does anybody know what mistake I'm making ?
          > >
          > > Thanks,
          > > __
          > >
          > > Ranjit
          > >
          > >[/color]
          >[/color]
          _______________ _______________ _______________ _______________ _______________ _[color=blue]
          > ____[color=green]
          > >
          > > Ranjit Noronha
          > > Graduate Research Associate
          > > Dept. of Computer and Information Sciences
          > > The Ohio State University
          > > Phone: (614)477-9900
          > > E-mail: noronha@cis.ohi o-state.edu
          > >
          > >[/color]
          >[/color]
          _______________ _______________ _______________ _______________ _______________ _[color=blue]
          > ____[color=green]
          > >
          > >///[/color]
          > try changing
          > foo *f=new foo();
          > to
          > foo* f= new foo;
          >
          > also, void main should be int main.
          > Bernie
          >[/color]
          You will see that the data member named "a" is not initialized, returning
          garbage. When you forced the initialization to zero, the pointer f returned
          0 the value of a..
          Using VC7 the following code returns the value of the data member a.

          #
          #include <iostream>
          using namespace std;
          class foo {
          foo* p;
          public:
          foo():a(911){}
          int a;
          foo* operator->() ;
          };
          foo* foo::operator->() {
          cout<<"Hullo"<< endl;
          return this;
          }
          int main() {
          foo f;
          cout<<(f->a);
          }
          Hullo
          911Press any key to continue


          Comment

          • Unforgiven

            #6
            Re: overloading -&gt;

            Ron Natalie wrote:[color=blue]
            > ranjit mario noronha wrote:[color=green]
            >> foo *f=new foo();
            >> cout<<f->a;[/color]
            >
            > This doesn't even attempt to call foo::operator->(). You invoked
            > the operator on a foo*. You can't overload on pointers (only
            > classes and enums). If you had written:
            > foo f;
            > cout << f->a;
            > things might have been different (of course, with your operator-> it
            > still won't compile).[/color]

            And in case you're wondering, you can in fact make a call to the operator->
            if you have only a pointer to the class: dereference it first.
            foo *f = new foo;
            (*f)->a;

            But you'll still need to fix your other mistakes.

            --
            Unforgiven

            "Most people make generalisations "
            Freek de Jonge

            Comment

            • ranjit mario noronha

              #7
              Re: overloading -&gt;

              [color=blue]
              > #include <iostream>
              > using namespace std;
              > class foo {
              > foo* p;
              > public:
              > foo():a(911){}
              > int a;
              > foo* operator->() ;
              > };
              > foo* foo::operator->() {
              > cout<<"Hullo"<< endl;
              > return this;
              > }
              > int main() {
              > foo f;
              > cout<<(f->a);
              > }
              > Hullo
              > 911Press any key to continue
              >
              >
              >[/color]

              This is great !! Thanks a lot. But what I really am trying to achieve is
              to figure out which element or alternatively the address of the element
              which is being accessed. For example for foo->a, the operator should be
              able to tell that the element a is being accesses.


              Thanks for responding,

              Ranjit

              Comment

              • Bernd Jochims

                #8
                Re: overloading -&gt;


                "ranjit mario noronha" <noronha@kappa. cis.ohio-state.edu> wrote in message
                news:Pine.GSO.4 .40.03091016392 70.20023-100000@kappa.ci s.ohio-state.edu...[color=blue]
                >[color=green]
                > > #include <iostream>
                > > using namespace std;
                > > class foo {
                > > foo* p;
                > > public:
                > > foo():a(911){}
                > > int a;
                > > foo* operator->() ;
                > > };
                > > foo* foo::operator->() {
                > > cout<<"Hullo"<< endl;
                > > return this;
                > > }
                > > int main() {
                > > foo f;
                > > cout<<(f->a);
                > > }
                > > Hullo
                > > 911Press any key to continue
                > >
                > >
                > >[/color]
                >
                > This is great !! Thanks a lot. But what I really am trying to achieve is
                > to figure out which element or alternatively the address of the element
                > which is being accessed. For example for foo->a, the operator should be
                > able to tell that the element a is being accesses.
                >
                >
                > Thanks for responding,
                >
                > Ranjit
                >[/color]
                I'm not sure what you are try to do, but this is what I came up with.
                #include <iostream>
                #include <vector>
                using namespace std;
                class foo {
                public:
                int a,b;
                vector<int>vi;
                foo(int x = 5763,int n=911):a(n),b(x ){
                vi.push_back(50 );
                vi.push_back(18 );
                vi.push_back(36 );
                }
                foo* operator->(){
                cout<<"Hello from ->"<<endl;
                return this;
                }
                int* operator ->*(int *n)
                {
                cout<<"hello from ->*"<<endl;
                return n;
                }
                };
                int main() {
                foo f,g;
                cout<<(f->a)<<" address of f.a "<<&f.a<<en dl;
                cout<<(f->*(&f.a))<<" address of f.a "<<&f.a<<en dl;
                cout<<(f->b)<<" address of f.b "<<&f.b<<en dl;
                cout<<(f->*(&f.b))<<" address of f.b "<<&f.b<<en dl;
                cout<<(g->*(&g.a))<<" address of g.a "<<&g.a<<en dl;
                for (size_t i=0;i<g->vi.size();i+ +)
                cout<<(g->*(&g.vi[i]))<<" address of "<<g.vi[i]<<endl;;
                }
                ouput vc7
                Hello from ->
                911 address of f.a 0012FEB4
                hello from ->*
                0012FEB4 address of f.a 0012FEB4
                Hello from ->
                5763 address of f.b 0012FEB8
                hello from ->*
                0012FEB8 address of f.b 0012FEB8
                hello from ->*
                0012FE90 address of g.a 0012FE90
                Hello from ->
                hello from ->*
                002F1130 address of 50
                Hello from ->
                hello from ->*
                002F1134 address of 18
                Hello from ->
                hello from ->*
                002F1138 address of 36
                Hello from ->
                Press any key to continue

                Bernie


                Comment

                Working...