[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: KSharedPtr changes
From:       "'Richard Smith'" <richard () metafoo ! co ! uk>
Date:       2006-01-15 17:25:23
Message-ID: 20022.62.253.128.12.1137345923.squirrel () www ! cantabgold ! net
[Download RAW message or body]

> On Saturday 14 January 2006 08:05, André Wöbbeking wrote:
>> On Saturday 14 January 2006 13:06, Boudewijn Rempt wrote:
>> > On Saturday 14 January 2006 12:54, Michel Hermier wrote:
>> > > One main disaventage of that is that we can't use the
>> > > "KSharedPtr<Foo> foo = bar;" syntax.
>> > > We must use the "KSharedPtr<Foo> foo(bar);" syntax.
>> >
>> > I'd hate that... We use this construction in so many places in Krita
>> > that it's going to be decided unfun to convert it everywhere.
>>
>> It's worth this little inconvenience. The dangling pointer from Michel's
>> example is much more anoying.
>
> Have you guys actually tested that?  Unless I'm mistaken, in C++, *when
> constructing an object*, the Foo foo = bar; and Foo foo(bar); syntaxes are
> identical and equivalent.

Not quite.

"Foo foo = bar;" is called copy-initialization, and it's more-or-less
equivalent to "Foo foo(static_cast<Foo>(bar));". That is, it constructs a
Foo temp from the object bar, then calls the Foo copy constructor to
construct foo (although this copy is allowed to be elided).

"Foo foo(bar);" is called direct-initialization and calls the Foo
constructor directly.

One difference is that copy-initialization doesn't allow for explicit
constructors, another is that it may be slower (if the copy constructor
call is not elided). A third difference is that not all types are
copy-constructible (and so copy-initialization won't work for them).

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic