Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > object copy with reference

Reply
Thread Tools

object copy with reference

 
 
Werner
Guest
Posts: n/a
 
      09-27-2011
On Sep 27, 8:34*am, Werner <(E-Mail Removed)> wrote:
> On Sep 26, 9:02*pm, Philipp Kraus <(E-Mail Removed)> wrote:
>
>
>
> > On 2011-09-26 10:26:23 +0200, Werner said:

>
> > > On Sep 25, 1:43*pm, Philipp Kraus <(E-Mail Removed)> wrote:
> > >> Hello,

>
> > >> I need a tip / hint for solving the following problem:

>
> > >> I have a class method that is run in different threads:
> > >> myclass::mythreadmethod( mysuperclass& )

>
> > >> mysuperclass is a reference variable, that is used during the threadis
> > >> running.
> > >> Anyway the method should be myclass::mythreadmethod( mysuperclass ),so that
> > >> each running thread has a own local copy of the object, but I can't
> > >> switch the parameter
> > >> because the method is derivated of another class.

>
> > >> I need in the method a local (deep-copy) of my object. So I create a
> > >> clone method like
> > >> myclass::mythreadmethod( mysuperclass& pobj )
> > >> {
> > >> * * mysuperclass& lobj = p_obj.clone();

>
> > >> }

>
> > >> In this case I get the compiler error, that I try to set up a temporary
> > >> assignment.

>
> > >> The try to do it like
> > >> myclass::mythreadmethod( mysuperclass& pobj )
> > >> {
> > >> * * mysuperclass& lobj;
> > >> * *p_obj.clone(lobj);}

>
> > >> create the message l_obj isn't initializate.

>
> > >> Does anyone has some tips to create a working solution? The perfect solution
> > >> should be a deep-copy of the pobj reference

>
> > >> Thanks

>
> > >> Phil

>
> > > How about making mysuperclass a pointer (or a smart pointer of some
> > > kind), then you can initialize it to zero (or in the case of smart_ptr
> > > it happens automatically). I'd use scoped_ptr in this case..., but as
> > > example I'd this use a bald pointer...

>
> > Thanks, I#M using smart-pointer, i don't see the solution with pointer

>
> Just be careful of auto_ptr in general. If possible, use scoped_ptr,
> otherwise be sure to overload your copy constructor and assignment
> operator.
>
> Kind regards,
>
> Werner


I meant to say "override" the copy constructor / assignment operator,
not "overload" (for those who are linguistically pedantic - I realize
the difference).

Kind regards,

Werner
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      09-27-2011
On 27 Sep., 11:08, Werner wrote:
> On Sep 27, 8:34*am, Werner wrote:
>
> > Just be careful of auto_ptr in general. If possible, use scoped_ptr,
> > otherwise be sure to overload your copy constructor and assignment
> > operator.

>
> I meant to say "override" the copy constructor / assignment operator,
> not "overload" (for those who are linguistically pedantic - I realize
> the difference).


Linguistic pedants would say "declare", I guess. Overriding is
something specific to virtual functions.

SG
 
Reply With Quote
 
 
 
 
Werner
Guest
Posts: n/a
 
      09-27-2011
On Sep 27, 1:10*pm, SG <(E-Mail Removed)> wrote:
> On 27 Sep., 11:08, Werner wrote:
>
> > On Sep 27, 8:34*am, Werner wrote:

>
> > > Just be careful of auto_ptr in general. If possible, use scoped_ptr,
> > > otherwise be sure to overload your copy constructor and assignment
> > > operator.

>
> > I meant to say "override" the copy constructor / assignment operator,
> > not "overload" (for those who are linguistically pedantic - I realize
> > the difference).

>
> Linguistic pedants would say "declare", I guess. Overriding is
> something specific to virtual functions.
>
> SG


. Agreed. Declare it, and declare it private (without
definition) if you want to prohibit copying and assignment,
or inherit from boost::noncopyable (which shows intent), or
just use a scoped_ptr, which is not copyable by default.

Apologies for my lack of linguistic accuracy. Thanks for
the correction.

Kind regards,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments