What do they call this kind of constructor?

V

Victor Bazarov

Dan said:
That is erroneous. A default constructor is a constructor without any
arguments.

Nit-pick again: a default constructor is one that can be invoked
without providing any arguments:

struct A {
A(int = 0); // default constructor
};

A a; // default-initialisation (actually A a(0); )
A aa(42); // parameterized

Victor
 
D

Dan Cernat

The size, in this case, doesn't matter. I can come up with an
implementation that even the string has one character, the copy
constructor could take forever to construct the object (eg it
calculates the billionth decimal of the PI number). Would you pass it
by value?

Dan
 
C

Clark S. Cox III

Nit-pick again: a default constructor is one that can be invoked
without providing any arguments:

struct A {
A(int = 0); // default constructor
};

A a; // default-initialisation (actually A a(0); )
A aa(42); // parameterized


One illustration of that that I particularly like:

struct A
{
explicit A(int);

/*This constructor is both the default
constructor *and* a copy constructor*/
A(const A &a = A(123));
};

A a; //Equivalent to A a(A(123))
 
V

Victor Bazarov

Clark said:
One illustration of that that I particularly like:

struct A
{
explicit A(int);

/*This constructor is both the default
constructor *and* a copy constructor*/
A(const A &a = A(123));
};

A a; //Equivalent to A a(A(123))

Interesting example. Is there a real need in something like that?
I do realize the need for 'explicit A(int)' and a copy c-tor in
general, but make a copy c-tor a default c-tor? Would it be better
to make the parameterized one the default?

struct A
{
explicit A(int = 123);

A(const A &a);
};

A a; // equivalent to A a(123)

or am I missing something WRT 'explicit' in that case?

V
 
J

Jason Heyes

Richard Herring said:
No. What does the standard say about it, and what aspect of that do you
think would make it better to pass by value than reference?

Bjarne Stroustrup says in his book that "many common uses of strings are
better served by implementations that minimize copying, use no free store
for short strings, ..., etc." So if the implementation is good, passing
short strings by value is fine and appropriate. If not, a better
implementation should be used.
 
J

Jason Heyes

Dan Cernat said:
The size, in this case, doesn't matter. I can come up with an
implementation that even the string has one character, the copy
constructor could take forever to construct the object (eg it
calculates the billionth decimal of the PI number). Would you pass it
by value?

If you do that, then you are a poor programmer. This is your fault, not
mine. ;)
 
D

Dan Cernat

Well, it works both ways: if you pass strings by value, then you are a
poor programmer.

Tink of it. You are writing a generic function. You cannot control what
is passed to it. In your application you might know what values are you
dealing with, but in a team environment someone could take your class,
use it in a different application and pass to your function some huge
strings. Big performance problems! Then, they look at the code and,
wow! Jason is passing strings by value! Does that make you a good
programmer?

And BTW it is not only about strings.

Have a nice weekend.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Jason said:
Bjarne Stroustrup says in his book that "many common uses of strings are
better served by implementations that minimize copying, use no free store
for short strings, ..., etc." So if the implementation is good, passing
short strings by value is fine and appropriate. If not, a better

But why do you need to care if the function will be always called with short
strngs or not?
 
C

Clark S. Cox III

Interesting example. Is there a real need in something like that?

Not that I can think of.
I do realize the need for 'explicit A(int)' and a copy c-tor in
general, but make a copy c-tor a default c-tor? Would it be better
to make the parameterized one the default?

In "real life", yes.
struct A
{
explicit A(int = 123);

A(const A &a);
};

A a; // equivalent to A a(123)

or am I missing something WRT 'explicit' in that case?

No, you're not missing anything. The example's really only there to
evoke a "hey that's interesting" response. And I only put the
"explicit" there out of habit (i.e. unless I have a reason to do
otherwise, I make *all* single parameter constructors explicit).
 
D

Default User

Dan said:
The size, in this case, doesn't matter. I can come up with an
implementation that even the string has one character, the copy
constructor could take forever to construct the object (eg it
calculates the billionth decimal of the PI number). Would you pass it
by value?



Please quote a relevant portion of the previous message when replying.
To do so from the Google interface, don't use the Reply at the bottom
of the message. Instead, click "show options" and use the Reply shown
in the expanded headers.

Brian
 
J

Jason Heyes

Dan Cernat said:
Well, it works both ways: if you pass strings by value, then you are a
poor programmer.

I already explained that passing by value is fine for small strings.
Tink of it. You are writing a generic function. You cannot control what
is passed to it. In your application you might know what values are you
dealing with, but in a team environment someone could take your class,
use it in a different application and pass to your function some huge
strings. Big performance problems! Then, they look at the code and,
wow! Jason is passing strings by value! Does that make you a good
programmer?

Why you talk about generic programming is beyond me.
And BTW it is not only about strings.

Actually, it is.
 
P

Peter Koch Larsen

Jason Heyes said:
I already explained that passing by value is fine for small strings.

It might well be fine, but it is more a question of habit and documentation.

/Peter

[snip]
 
J

Jason Heyes

Peter Koch Larsen said:
It might well be fine, but it is more a question of habit and
documentation.

Do you mind saying a bit more? I don't understand the problem.
 
R

Richard Herring

Jason Heyes said:
Bjarne Stroustrup says in his book that "many common uses of strings are
better served by implementations that minimize copying, use no free store
for short strings, ..., etc."

Indeed he does. My question was about the standard, which doesn't.
So if the implementation is good, passing
short strings by value is fine and appropriate.

And may well *still* be less efficient than passing by constant
reference.
If not, a better
implementation should be used.

It must be an exhilarating experience to live in a world where you have
a free choice of library implementations, constrained only by how
efficiently they copy short strings.
 

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

No members online now.

Forum statistics

Threads
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top