style of constructor initializer

M

Metaosp

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,
 
D

dc

This type of initialization is the only way when a class has reference
or const variable as its member variable.
 
R

Richard Herring

dc said:
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...


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

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.
 
B

Ben Pope

Metaosp said:
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
 
D

Daniel T.

Metaosp said:
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...
 
G

Gavin Deane

Ben said:
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
 
S

shankha

this type of argument passing is known as initializer list . this is
the standard everywhere . Look up in strousup
 
S

Sumit Rajan

shankha said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top