Which operator ~

C

Csaba

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 ?
 
V

Victor Bazarov

Csaba said:
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
 
C

Csaba

foo foo::eek:perator ~() const


return foo( ~f.integer_ );

Duh ! Serves me right for not using copy&paste from the real source :-(
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top