C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Lazy copy Semantics

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
mdlinux7@yahoo.co.in
Guest





PostPosted: Mon Oct 10, 2005 10:24 am    Post subject: Lazy copy Semantics Reply with quote



Hi Experts,

Is it possible to enforce compiler to invoke lazy copy semantics
automatically when left object is constant ?

Suppose I need to write class MyString and I want that lazy copy
semantic should be used for following function :

void strlength(const MyString s_);

But for those functions which modifies an object should get their own
object without impacting right hand side object.

What operator needs to be implemented if I want that if u declare the
pass-by-value parameter as const, a new memory allocation should not
occur and the same memory location should be used to optimize the code.

Regards
Dinesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Risto Lankinen
Guest





PostPosted: Mon Oct 10, 2005 11:14 am    Post subject: Re: Lazy copy Semantics Reply with quote




<mdlinux7 (AT) yahoo (DOT) co.in> wrote

Quote:

What operator needs to be implemented if I want that if u declare the
pass-by-value parameter as const, a new memory allocation should not
occur and the same memory location should be used to optimize the code.

To reply your question: C++ does not have const
constructors. Therefore, it is not possible to have
separate constructors for const and non-const object.

To offer a solution to your problem: Pass any const
parameter as a reference instead of value.

- Risto -


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
mdlinux7@yahoo.co.in
Guest





PostPosted: Mon Oct 10, 2005 8:02 pm    Post subject: Re: Lazy copy Semantics Reply with quote



Thanks for your reply. I asked this question because i was little
confused when I read Gotw#81 [constant optimization] which states below
lines

If so, and if you happen to use the smartened-up operator[ ]() or
iterators, and you declare the pass-by-value parameter as const, then
-- wonder of wonders! -- the String can, with no further help from you,
automagically and wholesomely optimize your code by avoiding a deep
copy...

That's why I was looking for a way to enforce compiler to use lazy
copying if you declare pass-by-value parameter as const.

Regards
Dinesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Oct 13, 2005 10:59 pm    Post subject: Re: Lazy copy Semantics Reply with quote

In article <Upr2f.11186$Nb2.196751 (AT) news1 (DOT) nokia.com>, Risto Lankinen
<rlankine (AT) hotmail (DOT) com> writes
Quote:
To reply your question: C++ does not have const
constructors. Therefore, it is not possible to have
separate constructors for const and non-const object.

At least not yet but there is a proposal to add them to the next C++
Standard.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Axter
Guest





PostPosted: Fri Oct 14, 2005 5:35 pm    Post subject: Re: Lazy copy Semantics Reply with quote

[email]mdlinux7 (AT) yahoo (DOT) co.in[/email] wrote:
Quote:
Thanks for your reply. I asked this question because i was little
confused when I read Gotw#81 [constant optimization] which states below
lines

If so, and if you happen to use the smartened-up operator[ ]() or
iterators, and you declare the pass-by-value parameter as const, then
-- wonder of wonders! -- the String can, with no further help from you,
automagically and wholesomely optimize your code by avoiding a deep
copy...

That's why I was looking for a way to enforce compiler to use lazy
copying if you declare pass-by-value parameter as const.

You could try using a COW (Copy On Write) smart pointer.
Check out the following class:
http://code.axter.com/cow_ptr.h

You can pass by value the above smart pointer, and it will do reference
counting untill an attempt is made to access the pointer.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.