Connecting Tech Pros Worldwide Forums | Help | Site Map

What do they call this kind of constructor?

Jason Heyes
Guest
 
Posts: n/a
#1: Jul 23 '05
Is there a special name for a constructor that does struct-like
initialisation? Example:

class DefaultCtor
{
std::string class_name;
bool compiler_generated;

public:
DefaultCtor(std::string class_name, bool compiler_generated);

};

DefaultCtor::DefaultCtor(std::string class_name_, bool compiler_generated_)
: class_name(class_name_), compiler_generated(compiler_generated_)
{
}

Thanks.



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: What do they call this kind of constructor?


Jason Heyes wrote:[color=blue]
> Is there a special name for a constructor that does struct-like
> initialisation?[/color]

What's a "struct-like initialisation"?
[color=blue]
> Example:
>
> class DefaultCtor
> {
> std::string class_name;
> bool compiler_generated;
>
> public:
> DefaultCtor(std::string class_name, bool compiler_generated);
>
> };
>
> DefaultCtor::DefaultCtor(std::string class_name_, bool compiler_generated_)
> : class_name(class_name_), compiler_generated(compiler_generated_)
> {
> }[/color]

What you have here is a normal _parameterized_ constructor. It is
*definitely* not a 'default' one, so there is a misnomer.

Also, as a side note, you shouldn't be passing 'std::string' by value,
there is no value in that (pun intended). You should pass it by a ref
to const:

SomeCtor(std::string const& class_name, ...

V
Jason Heyes
Guest
 
Posts: n/a
#3: Jul 23 '05

re: What do they call this kind of constructor?


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:jTXpe.81962$NC6.3179@newsread1.mlpsca01.us.to .verio.net...[color=blue]
> Jason Heyes wrote:[color=green]
>> Is there a special name for a constructor that does struct-like
>> initialisation?[/color]
>
> What's a "struct-like initialisation"?
>[color=green]
> > Example:
>>
>> class DefaultCtor
>> {
>> std::string class_name;
>> bool compiler_generated;
>>
>> public:
>> DefaultCtor(std::string class_name, bool compiler_generated);
>>
>> };
>>
>> DefaultCtor::DefaultCtor(std::string class_name_, bool
>> compiler_generated_) : class_name(class_name_),
>> compiler_generated(compiler_generated_)
>> {
>> }[/color]
>
> What you have here is a normal _parameterized_ constructor. It is
> *definitely* not a 'default' one, so there is a misnomer.
>
> Also, as a side note, you shouldn't be passing 'std::string' by value,
> there is no value in that (pun intended). You should pass it by a ref
> to const:
>
> SomeCtor(std::string const& class_name, ...
>
> V[/color]

I will call it a "default constructor that takes arguments" or a "default
parameterized constructor." How does that sound?

I don't pass std::string by const reference when I know it won't affect
performance.


Dan Cernat
Guest
 
Posts: n/a
#4: Jul 23 '05

re: What do they call this kind of constructor?




Jason Heyes wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:jTXpe.81962$NC6.3179@newsread1.mlpsca01.us.to .verio.net...[color=green]
> > Jason Heyes wrote:[color=darkred]
> >> Is there a special name for a constructor that does struct-like
> >> initialisation?[/color]
> >
> > What's a "struct-like initialisation"?
> >[color=darkred]
> > > Example:
> >>
> >> class DefaultCtor
> >> {
> >> std::string class_name;
> >> bool compiler_generated;
> >>
> >> public:
> >> DefaultCtor(std::string class_name, bool compiler_generated);
> >>
> >> };
> >>
> >> DefaultCtor::DefaultCtor(std::string class_name_, bool
> >> compiler_generated_) : class_name(class_name_),
> >> compiler_generated(compiler_generated_)
> >> {
> >> }[/color]
> >
> > What you have here is a normal _parameterized_ constructor. It is
> > *definitely* not a 'default' one, so there is a misnomer.
> >
> > Also, as a side note, you shouldn't be passing 'std::string' by value,
> > there is no value in that (pun intended). You should pass it by a ref
> > to const:
> >
> > SomeCtor(std::string const& class_name, ...
> >
> > V[/color]
>
> I will call it a "default constructor that takes arguments" or a "default
> parameterized constructor." How does that sound?[/color]

You provided it, therefore it is NOT a default constructor.

In your opinion, how a non-default parametrized constructor would look
like?

Thanks,
Dan

Howard
Guest
 
Posts: n/a
#5: Jul 23 '05

re: What do they call this kind of constructor?



"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:42a8678c$0$29335$afc38c87@news.optusnet.com.a u...[color=blue][color=green][color=darkred]
>>>
>>> DefaultCtor::DefaultCtor(std::string class_name_, bool
>>> compiler_generated_) : class_name(class_name_),
>>> compiler_generated(compiler_generated_)
>>> {
>>> }[/color]
>>
>> What you have here is a normal _parameterized_ constructor. It is
>> *definitely* not a 'default' one, so there is a misnomer.
>>
>> Also, as a side note, you shouldn't be passing 'std::string' by value,
>> there is no value in that (pun intended). You should pass it by a ref
>> to const:
>>
>> SomeCtor(std::string const& class_name, ...
>>
>> V[/color]
>
> I will call it a "default constructor that takes arguments" or a "default
> parameterized constructor." How does that sound?
>[/color]

A default constructor takes no arguments. Yours has arguments, so it's not
default. Why do you want to use the term "default" with it? It's simply a
(parameterized) constructor. Leave off the "default".

(By the way, who's going to hear/see you call it that? Is this for purposes
of discussion here, or for internal documentation purposes, or...?)

-Howard





Dan Cernat
Guest
 
Posts: n/a
#6: Jul 23 '05

re: What do they call this kind of constructor?


[color=blue]
> You provided it, therefore it is NOT a default constructor.[/color]

That is erroneous. A default constructor is a constructor without any
arguments.

/dan

Victor Bazarov
Guest
 
Posts: n/a
#7: Jul 23 '05

re: What do they call this kind of constructor?


Howard wrote:[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:42a8678c$0$29335$afc38c87@news.optusnet.com.a u...
>[color=green][color=darkred]
>>>>DefaultCtor::DefaultCtor(std::string class_name_, bool
>>>>compiler_generated_) : class_name(class_name_),
>>>>compiler_generated(compiler_generated_)
>>>>{
>>>>}
>>>
>>>What you have here is a normal _parameterized_ constructor. It is
>>>*definitely* not a 'default' one, so there is a misnomer.
>>>
>>>Also, as a side note, you shouldn't be passing 'std::string' by value,
>>>there is no value in that (pun intended). You should pass it by a ref
>>>to const:
>>>
>>> SomeCtor(std::string const& class_name, ...
>>>
>>>V[/color]
>>
>>I will call it a "default constructor that takes arguments" or a "default
>>parameterized constructor." How does that sound?
>>[/color]
>
>
> A default constructor takes no arguments.[/color]

Nit-pick: a default constructor is one that does not need arguments
specified to construct an object (IOW, it may have default argument
values). Both classes:

struct A {
A();
};

struct B {
B(int = 0);
};

have _default_ constructors.
[color=blue]
> Yours has arguments, so it's not
> default. Why do you want to use the term "default" with it? It's simply a
> (parameterized) constructor. Leave off the "default".
>
> (By the way, who's going to hear/see you call it that? Is this for purposes
> of discussion here, or for internal documentation purposes, or...?)[/color]

V
Jason Heyes
Guest
 
Posts: n/a
#8: Jul 23 '05

re: What do they call this kind of constructor?


"Howard" <alicebt@hotmail.com> wrote in message
news:n4_pe.301606$cg1.96748@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
>
> A default constructor takes no arguments. Yours has arguments, so it's
> not default. Why do you want to use the term "default" with it? It's
> simply a (parameterized) constructor. Leave off the "default".
>
> (By the way, who's going to hear/see you call it that? Is this for
> purposes of discussion here, or for internal documentation purposes,
> or...?)
>[/color]

I want the phrase "default parameterized" to mean "all arguments." This does
not affect the phrase "default" on its own, which means "no arguments."

I need to name various kinds of constructors. I want to write classes to
represent them. The classes must be given names.


Jason Heyes
Guest
 
Posts: n/a
#9: Jul 23 '05

re: What do they call this kind of constructor?


"Dan Cernat" <dcernat@excite.com> wrote in message
news:1118334035.112447.208820@o13g2000cwo.googlegr oups.com...[color=blue]
>
> You provided it, therefore it is NOT a default constructor.
>[/color]

Aren't you talking about compiler-generated constructors? Default
constructors take no arguments and do not have to be generated automatically
by a compiler.


vish
Guest
 
Posts: n/a
#10: Jul 23 '05

re: What do they call this kind of constructor?


BTW, Dont the default and non default constructors receive a pointer
("this") implicitly??

Victor Bazarov
Guest
 
Posts: n/a
#11: Jul 23 '05

re: What do they call this kind of constructor?


vish wrote:[color=blue]
> BTW, Dont the default and non default constructors receive a pointer
> ("this") implicitly??
>[/color]

Every non-static member function does. What does it have to do with
the problem at hand?
Peter Koch Larsen
Guest
 
Posts: n/a
#12: Jul 23 '05

re: What do they call this kind of constructor?



"Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
news:iB_pe.82048$NC6.30149@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> vish wrote:[color=green]
>> BTW, Dont the default and non default constructors receive a pointer
>> ("this") implicitly??
>>[/color]
>
> Every non-static member function does. What does it have to do with
> the problem at hand?[/color]

Technically, I would say no. A constructor does not receive a this-pointer -
it "creates" one. But this is nitpicking, of course.

/Peter


Shezan Baig
Guest
 
Posts: n/a
#13: Jul 23 '05

re: What do they call this kind of constructor?


Peter Koch Larsen wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
> news:iB_pe.82048$NC6.30149@newsread1.mlpsca01.us.t o.verio.net...[color=green]
> > vish wrote:[color=darkred]
> >> BTW, Dont the default and non default constructors receive a pointer
> >> ("this") implicitly??
> >>[/color]
> >
> > Every non-static member function does. What does it have to do with
> > the problem at hand?[/color]
>
> Technically, I would say no. A constructor does not receive a this-pointer -
> it "creates" one. But this is nitpicking, of course.
>
> /Peter[/color]


Really? The 'this' pointer exists even before the constructor starts
executing (e.g, you can use it in the initializer list), so I would
have guessed that it is passed to the constructor.

-shez-

Victor Bazarov
Guest
 
Posts: n/a
#14: Jul 23 '05

re: What do they call this kind of constructor?


Peter Koch Larsen wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
> news:iB_pe.82048$NC6.30149@newsread1.mlpsca01.us.t o.verio.net...
>[color=green]
>>vish wrote:
>>[color=darkred]
>>>BTW, Dont the default and non default constructors receive a pointer
>>>("this") implicitly??
>>>[/color]
>>
>>Every non-static member function does. What does it have to do with
>>the problem at hand?[/color]
>
>
> Technically, I would say no. A constructor does not receive a this-pointer -
> it "creates" one. But this is nitpicking, of course.[/color]

"Creates"? What does that mean? 'this' _pointer_ is valid before the
initialiser list is reached (although only as a pointer, the object has
not be constructed). Some other code allocates memory and therefore
"creates" the pointer, which is inserted into the expression any time
you use the 'this' keyword. That falls under "receive", not "create".

V
Old Wolf
Guest
 
Posts: n/a
#15: Jul 23 '05

re: What do they call this kind of constructor?


Jason Heyes wrote:[color=blue]
>
> I want the phrase "default parameterized" to mean "all arguments."
> This does not affect the phrase "default" on its own, which means
> "no arguments."
>
> I need to name various kinds of constructors.[/color]

This is like calling a green apple "red variant".
IOW, "default" means "no arguments", as you say. (NB. not to be
confused with "no parameters"). That is the essence of the term
"default". It does not make much sense to use "default ....." for
something with arguments.

Richard Herring
Guest
 
Posts: n/a
#16: Jul 23 '05

re: What do they call this kind of constructor?


In message <42a8678c$0$29335$afc38c87@news.optusnet.com.au> , Jason Heyes
<jasonheyes@optusnet.com.au> writes[color=blue]
>
>I don't pass std::string by const reference when I know it won't affect
>performance.
>[/color]
That would be "never", since you don't know how it's implemented.

--
Richard Herring
Jason Heyes
Guest
 
Posts: n/a
#17: Jul 23 '05

re: What do they call this kind of constructor?


"Richard Herring" <junk@[127.0.0.1]> wrote in message
news:R7OqXBNlYXqCFwTv@baesystems.com...[color=blue]
> In message <42a8678c$0$29335$afc38c87@news.optusnet.com.au> , Jason Heyes
> <jasonheyes@optusnet.com.au> writes[color=green]
>>
>>I don't pass std::string by const reference when I know it won't affect
>>performance.
>>[/color]
> That would be "never", since you don't know how it's implemented.
>[/color]

Have you considered how a std::string's size affects performance?


Peter Koch Larsen
Guest
 
Posts: n/a
#18: Jul 23 '05

re: What do they call this kind of constructor?



"Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
news:qc2qe.82105$NC6.955@newsread1.mlpsca01.us.to. verio.net...[color=blue]
> Peter Koch Larsen wrote:[color=green]
>> "Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
>> news:iB_pe.82048$NC6.30149@newsread1.mlpsca01.us.t o.verio.net...
>>[color=darkred]
>>>vish wrote:
>>>
>>>>BTW, Dont the default and non default constructors receive a pointer
>>>>("this") implicitly??
>>>>
>>>
>>>Every non-static member function does. What does it have to do with
>>>the problem at hand?[/color]
>>
>>
>> Technically, I would say no. A constructor does not receive a
>> this-pointer - it "creates" one. But this is nitpicking, of course.[/color]
>
> "Creates"? What does that mean? 'this' _pointer_ is valid before the
> initialiser list is reached (although only as a pointer, the object has
> not be constructed). Some other code allocates memory and therefore
> "creates" the pointer, which is inserted into the expression any time
> you use the 'this' keyword. That falls under "receive", not "create".
>
> V[/color]
I agree. What I meant to say was that while there is a this pointer, it does
not hold an object before the constructor completes. The this pointer
contains "raw" memory until that point.

/Peter


Richard Herring
Guest
 
Posts: n/a
#19: Jul 23 '05

re: What do they call this kind of constructor?


In message <42a989e7$0$14817$afc38c87@news.optusnet.com.au> , Jason Heyes
<jasonheyes@optusnet.com.au> writes[color=blue]
>"Richard Herring" <junk@[127.0.0.1]> wrote in message
>news:R7OqXBNlYXqCFwTv@baesystems.com...[color=green]
>> In message <42a8678c$0$29335$afc38c87@news.optusnet.com.au> , Jason Heyes
>> <jasonheyes@optusnet.com.au> writes[color=darkred]
>>>
>>>I don't pass std::string by const reference when I know it won't affect
>>>performance.
>>>[/color]
>> That would be "never", since you don't know how it's implemented.
>>[/color]
>Have you considered how a std::string's size affects performance?[/color]

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?

--
Richard Herring
Peter Koch Larsen
Guest
 
Posts: n/a
#20: Jul 23 '05

re: What do they call this kind of constructor?



"Shezan Baig" <shezanbaig2004@gmail.com> skrev i en meddelelse
news:1118351170.800476.241580@g47g2000cwa.googlegr oups.com...[color=blue]
> Peter Koch Larsen wrote:[color=green]
>> "Victor Bazarov" <v.Abazarov@comAcast.net> skrev i en meddelelse
>> news:iB_pe.82048$NC6.30149@newsread1.mlpsca01.us.t o.verio.net...[color=darkred]
>> > vish wrote:
>> >> BTW, Dont the default and non default constructors receive a pointer
>> >> ("this") implicitly??
>> >>
>> >
>> > Every non-static member function does. What does it have to do with
>> > the problem at hand?[/color]
>>
>> Technically, I would say no. A constructor does not receive a
>> this-pointer -
>> it "creates" one. But this is nitpicking, of course.
>>
>> /Peter[/color]
>
>
> Really? The 'this' pointer exists even before the constructor starts
> executing (e.g, you can use it in the initializer list), so I would
> have guessed that it is passed to the constructor.
>
> -shez-[/color]
You are correct. What I meant was that "this" does not point to an object in
the constructor. It points to raw memory which gets created while the
constructor runs.

/Peter


Victor Bazarov
Guest
 
Posts: n/a
#21: Jul 23 '05

re: What do they call this kind of constructor?


Dan Cernat wrote:[color=blue][color=green]
>>You provided it, therefore it is NOT a default constructor.[/color]
>
>
> That is erroneous. A default constructor is a constructor without any
> arguments.[/color]

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
Dan Cernat
Guest
 
Posts: n/a
#22: Jul 23 '05

re: What do they call this kind of constructor?


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

Clark S. Cox III
Guest
 
Posts: n/a
#23: Jul 23 '05

re: What do they call this kind of constructor?


On 2005-06-10 09:21:58 -0400, Victor Bazarov <v.Abazarov@comAcast.net> said:
[color=blue]
> Dan Cernat wrote:[color=green][color=darkred]
>>> You provided it, therefore it is NOT a default constructor.[/color]
>>
>> That is erroneous. A default constructor is a constructor without any
>> arguments.[/color]
>
> 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[/color]


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))

--
Clark S. Cox, III
clarkcox3@gmail.com

Victor Bazarov
Guest
 
Posts: n/a
#24: Jul 23 '05

re: What do they call this kind of constructor?


Clark S. Cox III wrote:[color=blue]
> On 2005-06-10 09:21:58 -0400, Victor Bazarov <v.Abazarov@comAcast.net>
> said:
>[color=green]
>> Dan Cernat wrote:
>>[color=darkred]
>>>> You provided it, therefore it is NOT a default constructor.
>>>
>>>
>>> That is erroneous. A default constructor is a constructor without any
>>> arguments.[/color]
>>
>>
>> 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[/color]
>
>
>
> 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))
>[/color]

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
Jason Heyes
Guest
 
Posts: n/a
#25: Jul 23 '05

re: What do they call this kind of constructor?


"Richard Herring" <junk@[127.0.0.1]> wrote in message
news:9CAx5iSeMZqCFw1v@baesystems.com...[color=blue]
> In message <42a989e7$0$14817$afc38c87@news.optusnet.com.au> , Jason Heyes
> <jasonheyes@optusnet.com.au> writes[color=green]
>>Have you considered how a std::string's size affects performance?[/color]
>
> 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?
>[/color]

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.


Jason Heyes
Guest
 
Posts: n/a
#26: Jul 23 '05

re: What do they call this kind of constructor?


"Dan Cernat" <dcernat@excite.com> wrote in message
news:1118410186.871868.34760@f14g2000cwb.googlegro ups.com...[color=blue]
> 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?
>[/color]

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


Dan Cernat
Guest
 
Posts: n/a
#27: Jul 23 '05

re: What do they call this kind of constructor?


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.

Julián Albo
Guest
 
Posts: n/a
#28: Jul 23 '05

re: What do they call this kind of constructor?


Jason Heyes wrote:
[color=blue]
> 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[/color]

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

--
Salu2
Clark S. Cox III
Guest
 
Posts: n/a
#29: Jul 23 '05

re: What do they call this kind of constructor?


On 2005-06-10 11:17:10 -0400, Victor Bazarov <v.Abazarov@comAcast.net> said:
[color=blue]
> Clark S. Cox III wrote:[color=green]
>> On 2005-06-10 09:21:58 -0400, Victor Bazarov <v.Abazarov@comAcast.net> said:
>>[color=darkred]
>>> Dan Cernat wrote:
>>>
>>>>> You provided it, therefore it is NOT a default constructor.
>>>>
>>>>
>>>> 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[/color]
>>
>>
>>
>> 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))
>>[/color]
>
> Interesting example. Is there a real need in something like that?[/color]

Not that I can think of.
[color=blue]
> 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?[/color]

In "real life", yes.
[color=blue]
>
> 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?[/color]

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


--
Clark S. Cox, III
clarkcox3@gmail.com

Default User
Guest
 
Posts: n/a
#30: Jul 23 '05

re: What do they call this kind of constructor?




Dan Cernat wrote:[color=blue]
> 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?[/color]



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

Jason Heyes
Guest
 
Posts: n/a
#31: Jul 23 '05

re: What do they call this kind of constructor?


"Julián Albo" <JULIANALBO@terra.es> wrote in message
news:42a9b7ea_2@x-privat.org...[color=blue]
>
> But why do you need to care if the function will be always called with
> short
> strngs or not?
>[/color]

I don't care, I know.


Jason Heyes
Guest
 
Posts: n/a
#32: Jul 23 '05

re: What do they call this kind of constructor?


"Dan Cernat" <dcernat@excite.com> wrote in message
news:1118418808.716739.152830@o13g2000cwo.googlegr oups.com...[color=blue]
> Well, it works both ways: if you pass strings by value, then you are a
> poor programmer.
>[/color]

I already explained that passing by value is fine for small strings.
[color=blue]
>
> 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?
>[/color]

Why you talk about generic programming is beyond me.
[color=blue]
>
> And BTW it is not only about strings.
>[/color]

Actually, it is.


Peter Koch Larsen
Guest
 
Posts: n/a
#33: Jul 23 '05

re: What do they call this kind of constructor?



"Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
news:42aa42b2$0$11688$afc38c87@news.optusnet.com.a u...[color=blue]
> "Dan Cernat" <dcernat@excite.com> wrote in message
> news:1118418808.716739.152830@o13g2000cwo.googlegr oups.com...[color=green]
>> Well, it works both ways: if you pass strings by value, then you are a
>> poor programmer.
>>[/color]
>
> I already explained that passing by value is fine for small strings.[/color]

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

/Peter

[snip][color=blue]
>
> Actually, it is.
>[/color]


Jason Heyes
Guest
 
Posts: n/a
#34: Jul 23 '05

re: What do they call this kind of constructor?


"Peter Koch Larsen" <pkspaml@mailme.dk> wrote in message
news:zAwqe.54263$Fe7.166587@news000.worldonline.dk ...[color=blue]
>
> "Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
> news:42aa42b2$0$11688$afc38c87@news.optusnet.com.a u...[color=green]
>> "Dan Cernat" <dcernat@excite.com> wrote in message
>> news:1118418808.716739.152830@o13g2000cwo.googlegr oups.com...[color=darkred]
>>> Well, it works both ways: if you pass strings by value, then you are a
>>> poor programmer.
>>>[/color]
>>
>> I already explained that passing by value is fine for small strings.[/color]
>
> It might well be fine, but it is more a question of habit and
> documentation.
>[/color]

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


Richard Herring
Guest
 
Posts: n/a
#35: Jul 23 '05

re: What do they call this kind of constructor?


In message <42a9b26d$0$15470$afc38c87@news.optusnet.com.au> , Jason Heyes
<jasonheyes@optusnet.com.au> writes[color=blue]
>"Richard Herring" <junk@[127.0.0.1]> wrote in message
>news:9CAx5iSeMZqCFw1v@baesystems.com...[color=green]
>> In message <42a989e7$0$14817$afc38c87@news.optusnet.com.au> , Jason Heyes
>> <jasonheyes@optusnet.com.au> writes[color=darkred]
>>>Have you considered how a std::string's size affects performance?[/color]
>>
>> 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?
>>[/color]
>
>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."[/color]

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

And may well *still* be less efficient than passing by constant
reference.
[color=blue]
>If not, a better
>implementation should be used.[/color]

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.

--
Richard Herring
Closed Thread