Bitwise AND overloading and argument list

Discussion in 'C++' started by mweltin@gmail.com, May 14, 2007.

  1. Guest

    Dear list memebers:
    I have been searching google and various lists but have come up with
    little that helps me. Perhaps it is my lack of diction that limits my
    search results. Be that as it may, I'm almost sure this question has
    been answered but I'll ask anyway in hopes that someone would point me
    in the right direction.

    My task is to overload the binary bitwise & operator. My class A, has
    as one of its members a long unsigned int, and when the this class is
    AND'ed with another long unsigned int I would like it to AND with the
    classes memeber. In fact I have this working

    Class A {
    public
    int b;
    long unsigned int c:
    long unsigned int operator&(unsigned long int right ){
    long unsigned int retval;
    retval = this->c & right;
    return retval;
    }
    ......
    };

    In my test program a call such as this works well

    A blah;
    x = long unsgined int.
    r = blah & x;

    but a call such as this fails to compile
    r = x & blah;

    I have made a non member function

    long unsigned int operator&(unsigned long int left, const A right )
    long unsigned int retval;
    retval = left & right.c ;
    return retval;
    }

    which comiplies into some object code (I complie my classes seperately
    as objects and then link them into
    my test program).

    I'm kind of at a loss for how to overload the AND binary operator in
    such a way that it is transparnent for the programer that uses my
    class. I would not like to have to put the left and right operands in
    a specific order, that is just not intuitive.

    thank you,
    Markus Weltin
     
    , May 14, 2007
    #1
    1. Advertising

  2. Guest

    I think I found my answer after all.
    http://groups.google.com/group/comp...erators argument list&rnum=9#6553840ea9aa0cdf
    suggests to declair the non member overloaded function as a "friend"
    within the A class.

    thank you,
    Markus

    wrote:
    > Dear list memebers:
    > I have been searching google and various lists but have come up with
    > little that helps me. Perhaps it is my lack of diction that limits my
    > search results. Be that as it may, I'm almost sure this question has
    > been answered but I'll ask anyway in hopes that someone would point me
    > in the right direction.
    >
    > My task is to overload the binary bitwise & operator. My class A, has
    > as one of its members a long unsigned int, and when the this class is
    > AND'ed with another long unsigned int I would like it to AND with the
    > classes memeber. In fact I have this working
    >
    > Class A {
    > public
    > int b;
    > long unsigned int c:
    > long unsigned int operator&(unsigned long int right ){
    > long unsigned int retval;
    > retval = this->c & right;
    > return retval;
    > }
    > .....
    > };
    >
    > In my test program a call such as this works well
    >
    > A blah;
    > x = long unsgined int.
    > r = blah & x;
    >
    > but a call such as this fails to compile
    > r = x & blah;
    >
    > I have made a non member function
    >
    > long unsigned int operator&(unsigned long int left, const A right )
    > long unsigned int retval;
    > retval = left & right.c ;
    > return retval;
    > }
    >
    > which comiplies into some object code (I complie my classes seperately
    > as objects and then link them into
    > my test program).
    >
    > I'm kind of at a loss for how to overload the AND binary operator in
    > such a way that it is transparnent for the programer that uses my
    > class. I would not like to have to put the left and right operands in
    > a specific order, that is just not intuitive.
    >
    > thank you,
    > Markus Weltin
     
    , May 15, 2007
    #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. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,451
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    478
    Fredrik Lundh
    Sep 30, 2005
  3. Ben Kial
    Replies:
    1
    Views:
    727
    Eric Enright
    Nov 15, 2004
  4. Dave Slayton
    Replies:
    1
    Views:
    207
    Ben Morrow
    Mar 11, 2007
  5. Jim Cochrane
    Replies:
    4
    Views:
    235
    Jim Cochrane
    Oct 3, 2007
Loading...

Share This Page