On Mon, 9 Aug 1999, Arnd Hanses wrote:

> >> I'm not sure why, but gcc -Wall doesn't give a damn warning in case of
> >> suspicious code like this:
> >> 
> >>         LString foo = (char*)bar;
> >> 
> >> Shouldn't there be one and how can this be achieved?
> >
> >The LString class overrides the asignment operator "=".  You do not
> >have to cast twice.
> 
> I've seen often the double cast in the code: 
> 
>          LString foo = LString (bar);

The only places I can remember seeing something like this is in default
parameter declarations such as:

        int something(LString foo = LString());

which is a much better way of saying that the default should be an empty
LString than saying:

        int something(LString foo = "");


> What would be the best coding style in those cases?

In general code there should be no need for explicitly assigning from a
constructor call and the following is what is really happening:

        LString foo(bar);

or

        LString foo = bar;

Any decent compiler will treat both the above definitions the same and
call a constructor.  It'd take a pretty lame compiler to treat the second
as
        LString foo(); foo = bar;

In fact,  I'm pretty sure the standard actually dictates that these two
cases are the same ie. they are both constructions;  the second is _not_
an empty construction followed by an assignment.


That said there are occasions when you're writing long assignments to
LStrings that require you to either split up your code or add an extra
LString constructor.  For example,

char const *foo = "Hello", *bar("Arnd!");
LString bizzo, whatsit(" there ");
...
bizzo = foo + whatsit + bar;                         // Error!!
bizzo = LString(foo) + whatsit + bar;                // Okay
bizzo = foo;   bizzo += whatsit + bar;               // Okay
bizzo = foo;   bizzo += whatsit;   bizzo += bar;     // Okay

The problem with the first one is that there is no

        LString operator+(char const &, LString const &)
or      LString operator+(char const *, LString const &)

so you can't add foo and whatsit together, but you can add whatsit and bar
together.  In any case it's faster to use the operator+= methods because
they can usually avoid at least one constructor call.

Allan. (ARRae)

Reply via email to