By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Which operator ~

P: n/a
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::operator ~() 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.
Jul 26 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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::operator ~() const
foo foo::operator ~() 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
Jul 26 '05 #2

P: n/a
Victor Bazarov <v.********@comAcast.net> wrote in
news:t2*******************@newsread1.mlpsca01.us.t o.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::operator ~() const


foo foo::operator ~() 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.
Jul 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.