Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy constructor and =

Reply
Thread Tools

Copy constructor and =

 
 
John Harrison
Guest
Posts: n/a
 
      08-16-2003

"E. Robert Tisdale" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> John Harrison wrote:
>
> > E. Robert Tisdale wrote:
> >
> >>John Harrison wrote:
> >>
> >>
> >>>Bit of a problem if rhs.N != N.
> >>
> >>Of course.
> >>But any discussion of exception handling here
> >>is a digression which could only obfuscate the point.

> >
> > I'm afraid I'm obfuscated already.
> > What point were you making with your bugged code?

>
> You are confused.


Clearly.

> There was no bug in my code.
>


Your class appeared to have no constructor other than a copy constructor.

Your assignment operator appeared not to deal correctly with the situation
where the objects were of different sizes. Though in mitagation it could be
said that it was impossible to construct such objects given the code you
quoted.

To me that seems bugged, but please explain. Maybe you mised out some code
that you thought wasn't relevant, or maybe I just don't understand.

> Unlike your example, my example addresses the case
> which concerned Gandalf -- classes that allocate dynamic memory.
>


My code intended was not to show exception safety, it was intended to show
that writing an assignment operator was easy once a copy constructor had
been written, which seemed to be the OP's situation.

john


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-16-2003

"E. Robert Tisdale" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> John Harrison wrote:
>
> > E. Robert Tisdale wrote:
> >
> >>John Harrison wrote:
> >>
> >>
> >>>Bit of a problem if rhs.N != N.
> >>
> >>Of course.
> >>But any discussion of exception handling here
> >>is a digression which could only obfuscate the point.

> >
> > I'm afraid I'm obfuscated already.
> > What point were you making with your bugged code?

>
> You are confused.
> There was no bug in my code.
>


I still don't understand the point of your code and you still haven't
explained. Bugged or not, what is the point of defining a copy constructor
in terms of an assignment operator? You seem to think it preferable (as far
as I can tell) but I think you should explain why.

john


 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      08-16-2003
John Harrison wrote:

> E. Robert Tisdale wrote:
>
>>John Harrison wrote:
>>
>>>E. Robert Tisdale wrote:
>>>
>>>>John Harrison wrote:
>>>>
>>>>>Bit of a problem if rhs.N != N.
>>>>
>>>>Of course.
>>>>But any discussion of exception handling here
>>>>is a digression which could only obfuscate the point.
>>>
>>>I'm afraid I'm obfuscated already.
>>>What point were you making with your bugged code?

>>
>>You are confused.
>>There was no bug in my code.

>
> I still don't understand the point of your code and you still haven't
> explained. Bugged or not, what is the point of defining a copy constructor
> in terms of an assignment operator? You seem to think it preferable (as far
> as I can tell) but I think you should explain why.


Unlike your example, my example addresses the case
which concerned Gandalf -- classes that allocate dynamic memory.

In this case, it is better to define operator= first
then use it to define the copy constructor.

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-17-2003
> >
> > I still don't understand the point of your code and you still haven't
> > explained. Bugged or not, what is the point of defining a copy

constructor
> > in terms of an assignment operator? You seem to think it preferable (as

far
> > as I can tell) but I think you should explain why.

>
> Unlike your example, my example addresses the case
> which concerned Gandalf -- classes that allocate dynamic memory.
>
> In this case, it is better to define operator= first
> then use it to define the copy constructor.
>


Why?

john


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-18-2003

"E. Robert Tisdale" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...

>
> X& X:perator=(const X& rhs) {
> for (size_t j = 0; j < N; ++j)
> A[j] = rhs[j];
> return *this;
> }


What happens when the assigned to value has a smaller value for
N than the right hand side?


 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments