KSharedPtr changes

'Richard Smith' richard at metafoo.co.uk
Sun Jan 15 17:25:23 GMT 2006

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

More information about the kde-core-devel mailing list