style of constructor initializer

Discussion in 'C++' started by Metaosp, Feb 13, 2006.

  1. Metaosp

    Metaosp Guest

    In the following code:

    class Foo {
    public:
    Foo(string& foo, string& bar) : foo(foo), bar(bar) {}
    private:
    string foo, bar;
    };

    Is this argument naming a common practice or considered evil? Or even
    an undefined behavior?


    Thanks,
    --
    Metaosp
    Metaosp, Feb 13, 2006
    #1
    1. Advertising

  2. Metaosp

    dc Guest

    This type of initialization is the only way when a class has reference
    or const variable as its member variable.
    dc, Feb 13, 2006
    #2
    1. Advertising

  3. In message <>, dc
    <> writes
    >This type of initialization is the only way when a class has reference
    >or const variable as its member variable.
    >

    This type of reply is not helpful. Please find out how to *quote* what
    you're replying to.

    But in any case...


    >>In the following code:
    >>
    >> class Foo {
    >> public:
    >> Foo(string& foo, string& bar) : foo(foo), bar(bar) {}
    >> private:
    >> string foo, bar;
    >> };
    >>
    >>Is this argument naming


    .... the OP was asking about the _names_ of the arguments (being the same
    as those of the member variables) not the initialisation syntax.

    >>a common practice or considered evil?


    Personally I'd say it's mildly evil, not because it breaks any rules but
    simply because any code which prompts you to post here asking for
    opinions must have been slightly surprising, and that's bad, or at least
    a hint that there are better ways to do it.

    >>Or even an undefined behavior?
    >>



    --
    Richard Herring
    Richard Herring, Feb 13, 2006
    #3
  4. Metaosp

    Sharad Kala Guest

    "Richard Herring" <junk@[127.0.0.1]> wrote in message

    [snip]

    | >>Or even an undefined behavior?

    To OP: No

    Sharad
    Sharad Kala, Feb 13, 2006
    #4
  5. Metaosp

    Ben Pope Guest

    Metaosp wrote:
    > In the following code:
    >
    > class Foo {
    > public:
    > Foo(string& foo, string& bar) : foo(foo), bar(bar) {}
    > private:
    > string foo, bar;
    > };
    >
    > Is this argument naming a common practice or considered evil? Or even
    > an undefined behavior?


    It's defined. It could be confusing for anybody reading it.

    I, and many others prefer to append an underscore to member variable names:

    class Foo {
    public:
    Foo(const std::string& foo, const std::string& bar) :
    foo_(foo),
    bar_(bar)
    {}
    private:
    std::string foo_;
    std::string bar_;
    };

    Since we're talking about style: I've also changed the indentation, the
    arguments to const and added the std namespace to things in the std
    namespace.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Feb 13, 2006
    #5
  6. Metaosp

    Daniel T. Guest

    In article <1139822038.9324.30.camel@jupiter>,
    Metaosp <> wrote:

    > In the following code:
    >
    > class Foo {
    > public:
    > Foo(string& foo, string& bar) : foo(foo), bar(bar) {}
    > private:
    > string foo, bar;
    > };
    >
    > Is this argument naming a common practice or considered evil? Or even
    > an undefined behavior?


    The behavior is defined, but the style doesn't seem to common. It can
    get you into trouble when you are writing the other functions and you
    find yourself using variable names that already exist in the object as
    parameters...

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Feb 13, 2006
    #6
  7. Metaosp

    Gavin Deane Guest

    Ben Pope wrote:

    > Metaosp wrote:
    > > In the following code:
    > >
    > > class Foo {
    > > public:
    > > Foo(string& foo, string& bar) : foo(foo), bar(bar) {}
    > > private:
    > > string foo, bar;
    > > };
    > >
    > > Is this argument naming a common practice or considered evil? Or even
    > > an undefined behavior?

    >
    > It's defined. It could be confusing for anybody reading it.
    >
    > I, and many others prefer to append an underscore to member variable names:
    >
    > class Foo {
    > public:
    > Foo(const std::string& foo, const std::string& bar) :
    > foo_(foo),
    > bar_(bar)
    > {}
    > private:
    > std::string foo_;
    > std::string bar_;
    > };


    The appended underscore used to be my preferred way of distinguishing
    member variables from other variables (I like it better than a prefix
    like m_ ). A nice side effect was the disambiguation of the names in
    the constructor as compared to the OP's legal but potentially confusing
    code.

    But after a while, I discovered that this disambiguation in
    constructors was almost the _only_ benefit. Inside the bodies of the
    class's member functions, the wart distinguishing member variables made
    the code slightly harder to read and type for no gain.

    So I changed my style to put the wart on the constructor parameter and
    leave the member variable name untouched. Hence

    class Foo {
    public:
    Foo(const std::string& foo_, const std::string& bar_) :
    foo(foo_),
    bar(bar_)
    {}
    private:
    std::string foo;
    std::string bar;
    };

    Just my £0.02. YMMV.

    Gavin Deane
    Gavin Deane, Feb 13, 2006
    #7
  8. Metaosp

    shankha Guest

    this type of argument passing is known as initializer list . this is
    the standard everywhere . Look up in strousup
    shankha, Feb 14, 2006
    #8
  9. Metaosp

    Sumit Rajan Guest

    "shankha" <> wrote in message
    news:...
    > this type of argument passing is known as initializer list . this is
    > the standard everywhere . Look up in strousup



    Please quote what you are replying to. Besides, I think you misunderstood
    the OP's question.

    Regards,
    Sumit.
    --
    Sumit Rajan <>
    Sumit Rajan, Feb 14, 2006
    #9
    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. Razvan
    Replies:
    7
    Views:
    17,543
    Lee Fesperman
    Jul 4, 2004
  2. Ed Thompson

    static initializer vs constructor

    Ed Thompson, Oct 9, 2004, in forum: Java
    Replies:
    9
    Views:
    907
    Chris Uppal
    Oct 11, 2004
  3. Matthias Kaeppler

    Initializer vs. Constructor assignment

    Matthias Kaeppler, May 7, 2005, in forum: Java
    Replies:
    4
    Views:
    992
    Tor Iver Wilhelmsen
    May 8, 2005
  4. Chris K
    Replies:
    1
    Views:
    566
    Victor Bazarov
    Apr 17, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,202
Loading...

Share This Page