473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.
Jul 26 '05 #1
2 1172
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
24
by: Brad Marts | last post by:
I would like to have a function that takes as an argument a base class but performs differently depending on which type of derived class is passed. Can I tell which derived class is passed? For...
10
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now...
29
by: guru.slt | last post by:
"the c++ standard library, tutorial and reference" book, it says: ++i is faster than i++. the latter involves a temporary object because it must return the old value/object of i. for this reason,...
10
by: Mikael | last post by:
Hi! I have written a logging class for my project and I want to support the stream syntax, that is I would like to write like this in my code: Log log("MYPREFIX"); log << "This is a...
0
by: Jim Heavey | last post by:
I am not sure which control I should use to get the display that I am looking for. I have a data table which contains repeating groups. Within each of those repeating groups can have one or...
7
by: eduzea | last post by:
I have some code that compiles with gcc but fails with MS VC 8. I would like to know which one is following the C++ standard. istream& istr = false ? cin : ( * new ifstream("test.txt")) ; ...
5
by: Nitesh | last post by:
Objects of type X are basic building blocks of my application and there are a few functions which return XHandle (typedef X** XHandle). The task at hand needs to use a couple of these functions and...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.