Re: C++ 101 dumb question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"David Webber" <dave@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23H5iJ5$sHHA.4796@xxxxxxxxxxxxxxxxxxxxxxx

"Anthony Jones" <Ant@xxxxxxxxxxxxxxxx> wrote in message
news:ukC%23bl$sHHA.4548@xxxxxxxxxxxxxxxxxxxxxxx

And you need to understand that copying a pointer is not the same as
copying
the thing it points to (and/or that the default copy constructor of
CMyClass
copies a pointer.)

Yep I do comprehend the difference between a pointer and the data it
points
to.

In that case, I suspect you have some other misconception about the fate
of
the data pointed to by all your char * pointers and/or what happens when
you
include something like ...="Hello world" in the program.

What's a "default copy constructor" ?

If you have a CMyClass a;

The statements

CMyClass b = a;

or

CMyClass b(a);

invoke a "copy constructor" to construct b. If you don't provide one
explicitly,

What does the signature of a copy constructor look like?

CMyClass(CMyClass src);

OR

is it:-

CMyClass& operator=(CMyClass& src);

you are liable to get a default version of it supplied by the
compiler. Its action is to copy the data members of the class, member by
member. If (as in your case) one of those members is a pointer, then
you'll create a copy of the pointer, and not a copy of the data it points
to. When that data goes out of scope, you'll find yourself with a
pointer
to junk.

Right I get that. What if the member was an instance of class that does
have it's own copy constructor would the default copy constructor call that
to copy the member or would it blindly copy the members of that object as
well? That to me is the critical question.


So if a class contains a pointer to data which it *owns*, then the copy
constructor must make a copy of the data and set its pointer to point at
the
copy. [There are string classes which come complete with copy
constructors
which manage all that. If you use standard nul-terminated strings as in
C,
then you have to do it yourself.]

Ok got that.


(NB Functions which return a class will also call the copy constructor,
even though neither of the above statements occur explicitly in that form,
as a copy of the class being returned is placed on the stack for the
benefit
of the calling function. The same is true if you call a function with a
class as one of its arguments.)

Surely that could be optimised away. The existing object is already on the
stack. Are you saying a second copy gets added to the stack, then the
original has it's destructor called, then after returning execution to the
caller another copy is performed, then presumably this temporary copy in the
stack has it's destructor called?

Perhaps I'm showing my ignorance again but since the callee knows its to
return an object it has on the stack it could defer its destruction to the
caller. The caller could perform the copy constructor once then destroy the
object from the callee function.



Dave

--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm



.



Relevant Pages

  • Re: C++ 101 dumb question
    ... Its action is to copy the data members of the class, ... If one of those members is a pointer, ... have it's own copy constructor would the default copy constructor call ... as a copy of the class being returned is placed on the stack for the ...
    (microsoft.public.vc.language)
  • Re: Question about arrays in objects
    ... >>I want to create a 2d array as an object member. ... > handled in the constructor, you just can't use a plain old array. ... > that is where using a pointer would come in. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Linked List, Newbie question
    ... > list this pointer changes to the new element, ... a constructor for the Point object. ... the next member to NULL, so that you're sure it's always going to be NULL ... You should have a destructor for this class. ...
    (comp.lang.cpp)
  • Questions of copy constructor
    ... have a copy constructor and assignment operator. ... If a base class have a pointer member, ...
    (comp.lang.cpp)
  • Re: C++ 101 dumb question
    ... CMyClass ... If one of those members is a pointer, then you'll create a copy of the pointer, and not a copy of the data it points to. ... So if a class contains a pointer to data which it *owns*, then the copy constructor must make a copy of the data and set its pointer to point at the copy. ...
    (microsoft.public.vc.language)