473,796 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

nonmember vs member?

From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that
I should try to make functions nonfriend nonmembers when practical, but
then I came across this:

Bruce Eckel says about operator overloads in Thinking In C++ (2nd Ed -
http://www.codeguru.com/cpp/tic/tic0129.shtml)
"In general, if it doesn't make any difference, they should be members, to
emphasize the association between the operator and its class."

That seems like a valid point, but probably not as convincing as the ones
Sutter gives. For example, a nonmember nonfriend might be generic enough
to be usable outside of the class; no reason to unnecessarily hide the
logic inside the class.

Does Eckel's argument ever outweigh Sutter's?
-Erik

Jul 19 '05 #1
13 2445
"Erik Haugen" <ha****@Xenon.S tanford.EDU> wrote in message
news:Pi******** *************** *************** @Xenon.Stanford .EDU...
[...]
Does Eckel's argument ever outweigh Sutter's?


I don't like Eckel's argument at all. Scott Meyers also
talks about member vs. non-member, and sums it up more
or less like this: "non-member when it can, member when
it must". For instance, assignment operators have to be
members. Comparison operators don't. With free
functions, you get implicit conversions. With member
functions, *this will never get an implicit conversion.
There's more arguments, but that's enough for now.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #2

"Erik Haugen" <ha****@Xenon.S tanford.EDU> wrote in message
news:Pi******** *************** *************** @Xenon.Stanford .EDU...
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that
I should try to make functions nonfriend nonmembers when practical, but
then I came across this:

Bruce Eckel says about operator overloads in Thinking In C++ (2nd Ed -
http://www.codeguru.com/cpp/tic/tic0129.shtml)
"In general, if it doesn't make any difference, they should be members, to
emphasize the association between the operator and its class."

That seems like a valid point, but probably not as convincing as the ones
Sutter gives. For example, a nonmember nonfriend might be generic enough
to be usable outside of the class; no reason to unnecessarily hide the
logic inside the class.

Does Eckel's argument ever outweigh Sutter's?


I think it's a matter of connotations. I think Eckel's advice is good: "If
it doesn't make any difference...." . That seems vague enough that it's
pretty safe :-) On the other hand, I think the GotW advice is not so good:
"Where possible, prefer writing functions as nonmember nonfriends." That
seems too far reaching for me. If you read Eckel's book, you'll see that
one of the points of OO programming is to reduce the number of functions
that you have to call with a bunch of parameters, when encapsulation reduces
the need for parameters. The GotW advice seems to be consistent with
"generic programming" (similar principles to some of the template and STL
stuff.) But after all, can't almost *everything* be a nonmember nonfriend
function, if you send it enough parameters? I don't like their phrase
"where possible".
Jul 19 '05 #3

"David B. Held" <dh***@codelogi cconsulting.com > wrote in message
news:bn******** **@news.astound .net...
"Erik Haugen" <ha****@Xenon.S tanford.EDU> wrote in message
news:Pi******** *************** *************** @Xenon.Stanford .EDU...
[...]
Does Eckel's argument ever outweigh Sutter's?
I don't like Eckel's argument at all. Scott Meyers also
talks about member vs. non-member, and sums it up more
or less like this: "non-member when it can, member when
it must".


"more or less"? I don't agree. I think you're taking it out of context.
Here is what he says, more or less.

If the function is virtual, of course it must be a member. If the function
is the << or >> operator, make it non-member. If the function needs type
conversion on its left most argument, make it non-member (note this is
common in operators). Everything else should be a member function.

That's hardly "non-member when it can". In fact, the vast majority of
functions will be member by that rule. In fact, in Item 19 of "Effective
C++", operator* and operator<< and operator>> are the *only* functions he
uses at all to make his point.
For instance, assignment operators have to be
members. Comparison operators don't.


You're using operators as an example. The vast majority of OO functions
written are non-operator. The OP didn't ask anything about operators
specifically, just functions.
Jul 19 '05 #4
On Fri, 24 Oct 2003 02:28:14 -0700, Erik Haugen <ha****@Xenon.S tanford.EDU> wrote:
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that
I should try to make functions nonfriend nonmembers when practical, but
then I came across this:

Bruce Eckel says about operator overloads in Thinking In C++ (2nd Ed -
http://www.codeguru.com/cpp/tic/tic0129.shtml)
"In general, if it doesn't make any difference, they should be members, to
emphasize the association between the operator and its class."

That seems like a valid point, but probably not as convincing as the ones
Sutter gives. For example, a nonmember nonfriend might be generic enough
to be usable outside of the class; no reason to unnecessarily hide the
logic inside the class.

Does Eckel's argument ever outweigh Sutter's?


I do hope someone authoritative will clarify once and for all whether
two, three, four or eight character positions is best for indentation, and
also which indentation style to follow. In this regard, should I heed the
advice of Bjarne, Herb or Bruce? It's frustrating not to have clear
leadership in this important matter.

I am sure that with the correct, officially approved indentation everything
else will just fall into place of its own accord.

Jul 19 '05 #5
"Alf P. Steinbach" escribió:
I do hope someone authoritative will clarify once and for all whether
two, three, four or eight character positions is best for indentation, and
also which indentation style to follow. In this regard, should I heed the
advice of Bjarne, Herb or Bruce? It's frustrating not to have clear
leadership in this important matter.


Were is the importance? I can read code written with any reasonable
standard without problem. And if you need to adapt foreign code to your
own standard you can use indent or something similar.

Regards.
Jul 19 '05 #6
On Fri, 24 Oct 2003 17:51:20 +0200, =?iso-8859-1?Q?Juli=E1n?= Albo <JU********@ter ra.es> wrote:
"Alf P. Steinbach" escribi=F3:
I do hope someone authoritative will clarify once and for all whether
two, three, four or eight character positions is best for indentation, =

and
also which indentation style to follow. In this regard, should I heed =

the
advice of Bjarne, Herb or Bruce? It's frustrating not to have clear
leadership in this important matter.


Were is the importance? I can read code written with any reasonable
standard without problem. And if you need to adapt foreign code to your
own standard you can use indent or something similar.


Ah, you're probably American.

Jul 19 '05 #7

"Alf P. Steinbach" <al***@start.no > wrote in message
news:3f******** ********@News.C IS.DFN.DE...

Does Eckel's argument ever outweigh Sutter's?


I do hope someone authoritative will clarify once and for all whether
two, three, four or eight character positions is best for indentation, and
also which indentation style to follow. In this regard, should I heed the
advice of Bjarne, Herb or Bruce? It's frustrating not to have clear
leadership in this important matter.


What on earth are you talking about? Is this some kind of weird troll?
Jul 19 '05 #8
"Alf P. Steinbach" escribió:
Ah, you're probably American.


Clean your cristal ball ;)

Regards.
Jul 19 '05 #9
jeffc wrote:
"Alf P. Steinbach" <al***@start.no > wrote in message
news:3f******** ********@News.C IS.DFN.DE...
Does Eckel's argument ever outweigh Sutter's?


I do hope someone authoritative will clarify once and for all whether
two, three, four or eight character positions is best for indentation, and
also which indentation style to follow. In this regard, should I heed the
advice of Bjarne, Herb or Bruce? It's frustrating not to have clear
leadership in this important matter.

What on earth are you talking about? Is this some kind of weird troll?


Well I thought it amusing.
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2844
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the dependency/independency on the lifecycle of the object, what is the significant differences between public member functions and public static member functions?
4
2500
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
1
1213
by: bluekite2000 | last post by:
The author's reason is "because its hard to write templates that dont know whether or not an operation is a member for a given type". What does he mean by this? An example is appreciated.
3
1475
by: bluekite2000 | last post by:
I d rather have a Matlab syntax such as: // create a Matrix of size 3,4 and fills it w/ random values. Matrix<double> M=rand(3,4) But since this function changes the state of the object, I ought to make it a member function. ie.
2
1983
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when objects of your type have a way to exchange their values more efficiently than via brute-force assignment, such as if they have their own swap or equivalent function. Additionally, consider specializing std::swap for your own template types:" ...
9
10850
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using LDIFDE as a comparison I get the same results. No members means just that, an empty group. Zero means that the DirectorySearcher.SizeLimit has been exceeded....
8
2161
by: desktop | last post by:
I have read that the allocator class has the following nonmember functions: template <class In, class ForFor uninitialized_copy(In,In,For); template <class For, class T> void uninitialized_fill(For, For, const T&); But what does it mean that the two functions are nonmembers? Are they defined outside the class declaration in the header file?
7
3816
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
7
3974
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member functions. Is it normal how C++ Compiler can compile large class without any problem? Didn't C++ Compiler have rules to limit the number of member functions? One big object has complex operations how member variables and member functions can be...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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 we have to send another system
2
3731
muto222
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.