[Joaqu�n Cuenca Abela explained scoping]

Martin Sevior wrote:

> Actually because of the scoping you've beautifully explained
> what we have are lots of instances of
>
> void foo(void)
> {
>   UT_String me;
>   if(x)
>   {
>      me = "bar:";
>   }
>   else
>   {
>      me = "foo:";
>   }
>   me += "fred; ";

which can changed to initializers:

    UT_String me(x ? "bar" : "foo");

or in more complex cases:

    ut_String me(get_char_ptr_depending_on_x(x));

or inlined:

    const char* psz;

    if (...)
    ...
    // lots of lines setting psz to something depending on another thing or
two

    UT_String me(psz);

Please note that this is the way (in fact the only way) to initialize
references, e.g. members like (module style and some keywords):

class bar_t;
extern bar_t& obj1;
extern bar_t& obj2;

class foo {
    foo(bool b) : m_bar(b ? obj1 : obj2) {}
    bar_t& m_bar;
}


It's always better to use references than pointers when possible.


/Mike


Reply via email to