nonmember vs member?

Discussion in 'C++' started by Erik Haugen, Oct 24, 2003.

  1. Erik Haugen

    Erik Haugen Guest

    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
     
    Erik Haugen, Oct 24, 2003
    #1
    1. Advertising

  2. "Erik Haugen" <> wrote in message
    news:p...
    > [...]
    > 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
     
    David B. Held, Oct 24, 2003
    #2
    1. Advertising

  3. Erik Haugen

    jeffc Guest

    "Erik Haugen" <> wrote in message
    news:p...
    > 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".
     
    jeffc, Oct 24, 2003
    #3
  4. Erik Haugen

    jeffc Guest

    "David B. Held" <> wrote in message
    news:bnat63$18i$...
    > "Erik Haugen" <> wrote in message
    > news:p...
    > > [...]
    > > 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.
     
    jeffc, Oct 24, 2003
    #4
  5. On Fri, 24 Oct 2003 02:28:14 -0700, Erik Haugen <> 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.
     
    Alf P. Steinbach, Oct 24, 2003
    #5
  6. "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.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 24, 2003
    #6
  7. On Fri, 24 Oct 2003 17:51:20 +0200, =?iso-8859-1?Q?Juli=E1n?= Albo <> 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.
     
    Alf P. Steinbach, Oct 24, 2003
    #7
  8. Erik Haugen

    jeffc Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    > >
    > >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?
     
    jeffc, Oct 24, 2003
    #8
  9. "Alf P. Steinbach" escribió:

    > Ah, you're probably American.


    Clean your cristal ball ;)

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 24, 2003
    #9
  10. Erik Haugen

    lilburne Guest

    jeffc wrote:
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    >
    >>>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.
     
    lilburne, Oct 24, 2003
    #10
  11. Erik Haugen

    Unforgiven Guest

    jeffc wrote:
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    >>>
    >>> 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?


    I think he is trying to say that what the OP asked is a similar kind of
    political debate, i.e. there's not one answer, and you can simply use your
    personal preference.

    --
    Unforgiven

    "Most people make generalisations"
    Freek de Jonge
     
    Unforgiven, Oct 24, 2003
    #11
  12. "jeffc" <> wrote in message
    news:...
    > [...]
    > 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.


    Ok, so I was thinking of Andrei's "intepretation" of Meyers'
    rule, which is: "Make everything a free function". ;)

    > [...]
    > 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.


    That's ambiguous. He first asked about functions, and then
    mentioned what Eckel's had to say about *operators*. So
    I assumed he was talking primarily about operators. In
    which case, I think Meyers mostly agrees with my summary
    (since most binary operators, for instance, benefit from
    implicit conversion, and most of the operators that you want
    to be members couldn't be free anyway).

    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
     
    David B. Held, Oct 24, 2003
    #12
  13. Erik Haugen

    jeffc Guest

    "David B. Held" <> wrote in message
    news:bnbvgf$78v$...
    > > 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.

    >
    > That's ambiguous. He first asked about functions, and then
    > mentioned what Eckel's had to say about *operators*. So
    > I assumed he was talking primarily about operators. In
    > which case, I think Meyers mostly agrees with my summary
    > (since most binary operators, for instance, benefit from
    > implicit conversion, and most of the operators that you want
    > to be members couldn't be free anyway).


    Yes, I suppose.
     
    jeffc, Oct 24, 2003
    #13
  14. Erik Haugen

    jeffc Guest

    "lilburne" <> wrote in message
    news:bnbs5v$vlbg1$-berlin.de...
    > jeffc wrote:
    > > "Alf P. Steinbach" <> wrote in message
    > > news:...
    > >
    > >>>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.


    Ah, in that case I guess it's pretty funny too.
     
    jeffc, Oct 24, 2003
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DJ Dev
    Replies:
    3
    Views:
    16,169
    Gandalf
    Feb 8, 2004
  2. Replies:
    1
    Views:
    277
    Victor Bazarov
    Jul 29, 2005
  3. nonmember function

    , Aug 1, 2005, in forum: C++
    Replies:
    3
    Views:
    300
    msalters
    Aug 3, 2005
  4. Replies:
    2
    Views:
    277
  5. desktop

    nonmember functions?

    desktop, May 23, 2007, in forum: C++
    Replies:
    8
    Views:
    394
    Gennaro Prota
    May 25, 2007
Loading...

Share This Page