 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Csaba Guest
|
Posted: Tue Jul 26, 2005 1:22 pm Post subject: Which operator ~ |
|
|
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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Jul 26, 2005 1:40 pm Post subject: Re: Which operator ~ |
|
|
Csaba wrote:
| Quote: | 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
| Quote: | {
return foo( ~integer_ );
}
2. Non-member operator:
foo operator ~ ( const foo& f )
{
return foo( f.integer_ );
|
return foo( ~f.integer_ );
| Quote: | }
(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...
| Quote: |
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?
| Quote: | Would that be sufficient reason to choose it ?
|
Not really.
V
|
|
| Back to top |
|
 |
Csaba Guest
|
Posted: Wed Jul 27, 2005 2:59 pm Post subject: Re: Which operator ~ |
|
|
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote in
news:t2rFe.21867$Tf5.18974 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net:
| Quote: | 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 :-(
| Quote: |
(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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|