Which operator ~

Discussion in 'C++' started by Csaba, Jul 26, 2005.

  1. Csaba

    Csaba Guest

    I have a class which contains an integer acting as a bit map (i.e.
    individual bits have separate meanings)

    union foo
    {
    int integer_;
    struct{ /* here be bitfields */ } parts_;

    foo( int i ) : integer( i ) {}
    // various operators here
    }

    I have implemented operators &= |= ^= as members and operator & | ^ as
    nonmembers (as recommeded in More Effective C++).

    I'm now wondering how to implement operator~
    I can see two possibilities:

    1. Member operator:

    foo::eek:perator ~() const
    {
    return foo( ~integer_ );
    }

    2. Non-member operator:

    foo operator ~ ( const foo& f )
    {
    return foo( f.integer_ );
    }

    (union behaves like struct: access is public by default, otherwise the
    nonmember would have to be a friend).

    The two are mutually exclusive; they both do the job. Which one should I
    choose ? Which one is better/cleaner/more canonical ?

    If I change the data memebrs to private:

    union foo
    {
    private:
    int integer_;
    struct{ /* here be bitfields */ } parts_;
    public:
    foo( int i ) : integer( i ) {}
    // various operators here
    }

    then having operator~ as a member is more convenient (no need to declare
    it as a friend because it already has access to the private members).
    Would that be sufficient reason to choose it ?

    --
    Life is complex, with real and imaginary parts.
    Csaba, Jul 26, 2005
    #1
    1. Advertising

  2. Csaba wrote:
    > I have a class which contains an integer acting as a bit map (i.e.
    > individual bits have separate meanings)
    >
    > union foo
    > {
    > int integer_;
    > struct{ /* here be bitfields */ } parts_;
    >
    > foo( int i ) : integer( i ) {}
    > // various operators here
    > }
    >
    > I have implemented operators &= |= ^= as members and operator & | ^ as
    > nonmembers (as recommeded in More Effective C++).
    >
    > I'm now wondering how to implement operator~
    > I can see two possibilities:
    >
    > 1. Member operator:
    >
    > foo::eek:perator ~() const


    foo foo::eek:perator ~() const

    > {
    > return foo( ~integer_ );
    > }
    >
    > 2. Non-member operator:
    >
    > foo operator ~ ( const foo& f )
    > {
    > return foo( f.integer_ );


    return foo( ~f.integer_ );

    > }
    >
    > (union behaves like struct: access is public by default, otherwise the
    > nonmember would have to be a friend).
    >
    > The two are mutually exclusive; they both do the job. Which one should I
    > choose ? Which one is better/cleaner/more canonical ?


    According to Meyers, the second would be more canonical. Any operator
    that doesn't change its operand should be non-member...

    >
    > If I change the data memebrs to private:
    >
    > union foo
    > {
    > private:
    > int integer_;
    > struct{ /* here be bitfields */ } parts_;
    > public:
    > foo( int i ) : integer( i ) {}
    > // various operators here
    > }
    >
    > then having operator~ as a member is more convenient (no need to declare
    > it as a friend because it already has access to the private members).


    How about the rest of the operators?

    > Would that be sufficient reason to choose it ?


    Not really.

    V
    Victor Bazarov, Jul 26, 2005
    #2
    1. Advertising

  3. Csaba

    Csaba Guest

    Victor Bazarov <> wrote in
    news:t2rFe.21867$01.us.to.verio.net:

    > Csaba wrote:
    >> I have a class which contains an integer acting as a bit map (i.e.
    >> individual bits have separate meanings)
    >>
    >> union foo
    >> {
    >> int integer_;
    >> struct{ /* here be bitfields */ } parts_;
    >>
    >> foo( int i ) : integer( i ) {}
    >> // various operators here
    >> }
    >>
    >> I have implemented operators &= |= ^= as members and operator & | ^
    >> as nonmembers (as recommeded in More Effective C++).
    >>
    >> I'm now wondering how to implement operator~
    >> I can see two possibilities:
    >>
    >> 1. Member operator:
    >>
    >> foo::eek:perator ~() const

    >
    > foo foo::eek:perator ~() const
    >
    >> {
    >> return foo( ~integer_ );
    >> }
    >>
    >> 2. Non-member operator:
    >>
    >> foo operator ~ ( const foo& f )
    >> {
    >> return foo( f.integer_ );

    >
    > return foo( ~f.integer_ );
    >
    >> }


    Duh ! Serves me right for not using copy&paste from the real source :-(

    >>
    >> (union behaves like struct: access is public by default, otherwise
    >> the nonmember would have to be a friend).
    >>
    >> The two are mutually exclusive; they both do the job. Which one
    >> should I choose ? Which one is better/cleaner/more canonical ?

    >
    > According to Meyers, the second would be more canonical. Any operator
    > that doesn't change its operand should be non-member...
    >


    Now *that* makes a lot of sense. Thanks.

    --
    Life is complex, with real and imaginary parts.
    Csaba, Jul 27, 2005
    #3
    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. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    560
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    410
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,032
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    4,998
    Jeff Schwab
    Mar 20, 2005
  5. Vivek Mandava
    Replies:
    28
    Views:
    2,155
    ArWeGod
    Sep 11, 2003
Loading...

Share This Page