Re: Object that transfers ownership on assignment/copy
Scott Gifford wrote:
"Victor Bazarov" <v.Abazarov@comAcast.net> writes:
Scott Gifford wrote:
[...]
C c_factory() {
return C(new string("foo"));
}
[...]
Here you have a temporary from which you're trying to construct
an object. That operation requires binding a non-const reference
to the temporary, which is prohibited.
I assume you mean the above snippet? I can fix this particular error
by replacing using a temporary:
C c_factory() {
C tmp(new string("foo"));
return tmp;
}
The variable you call 'tmp' has nothing to do with the "temporary" that
I refered to. A temporary object and an object with automatic storage
duration are not the same. Your 'tmp' here is the latter. The former
is created as the function returns a value.
but one error still remains:
test8.C: In function int main():
test8.C:33: error: no matching function for call to C::C(C)
test8.C:10: note: candidates are: C::C(C&)
test8.C:8: note: C::C(std::string*)
Take a look at how 'auto_ptr' is implemented and follow that instead
of trying to wrap it up.
The actual code contains more member variables and methods, so it's
not quite as simple as what I posted. Since the auto_ptr code is
nontrivial and I don't understand all of it, I suspect it would be too
error-prone to try and copy its implementation into my class. I will
probably contine using the "mutable" hack if there's not a
straightforward way to make this work.
In case you don't read my other reply, think reference counting instead
of transfer of ownership.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask