473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

style of constructor initializer

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
Feb 13 '06 #1
8 1569
dc
This type of initialization is the only way when a class has reference
or const variable as its member variable.

Feb 13 '06 #2
In message <11*********************@f14g2000cwb.googlegroups. com>, dc
<de************@gmail.com> 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
Feb 13 '06 #3

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

[snip]

| >>Or even an undefined behavior?

To OP: No

Sharad
Feb 13 '06 #4
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...
Feb 13 '06 #5
In article <1139822038.9324.30.camel@jupiter>,
Metaosp <me*****@gmail.com> 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.
Feb 13 '06 #6

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

Feb 13 '06 #7
this type of argument passing is known as initializer list . this is
the standard everywhere . Look up in strousup

Feb 14 '06 #8

"shankha" <sh*************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 <su****@msdc.hcltech.com>
Feb 14 '06 #9

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

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
9
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the...
22
by: San | last post by:
Hi, I wrote two simple constructors (one of them default) to initialize a student object. Student::Student(string name) { student_name = name; student_id = LATEST_ID + 1;...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
4
by: john | last post by:
Hey guys, Thank you for your input from the last topic. I tried referencing my c+ + book and googled the topic but i'am still confused a bit. I have another problem with that same code. I was...
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.