home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!sun-barr!ames!haven.umd.edu!darwin.sura.net!wupost!usc!sdd.hp.com!hp-cv!hp-pcd!hp-vcd!nuntius
- From: John Matthews <jm@vcd.hp.com>
- Subject: Default assignment semantics
- Message-ID: <BuJs8K.Hzr@vcd.hp.com>
- Sender: news@vcd.hp.com (News user)
- Organization: Hewlett-Packard
- X-Useragent: Nuntius v1.1a5
- Date: Mon, 14 Sep 1992 02:57:55 GMT
- Lines: 64
-
- As I understand it, if a class does not have an explicitly defined
- assignment operator, a default assignment operator is generated
- by the compiler. This default operator simply does a member-wise
- assignment. As has been pointed out many times, these semantics
- are dangerous for classes that have pointers to dynamically created
- objects as fields.
-
- It seems to me that a more robust default assignment operator would
- have the following semantics:
-
- 1) Invoke the destructor of the object being assigned to.
- 2) Invoke the copy constructor on the object being assigned
- to, taking as an argument the object being assigned from.
-
- In the case where a class has no user defined destructor or copy
- constructor, the above semantics devolves to member-wise
- assignment, just as before. However, for classes that have
- user defined destructors and copy constructors, (as all classes
- that have pointers to dynamic objects should) the above
- semantics generates code that is much safer. Take for example a
- String class, whose constructor allocates a character buffer for
- a string, and whose destructor frees that buffer. The user-defined
- copy constructor would construct its String object with a separately
- allocated character buffer of the same size as its String argument,
- and perform a string copy between the two character buffers.
-
- Under the current default assignment operator semantics, for every
- String assignment made, a character buffer is 'leaked', and two
- pointers to the same buffer are made. Under the above semantics,
- however, the assignee String first has its buffer deallocated, and
- then recieves a new buffer from the copy constructor, just like you
- would expect.
-
- Since this is not the semantics of the current assignment operator,
- a user defined assignment operator needs to be defined whenever
- a user defined destructor or copy constructor exists for a class.
- Thus what I want to do in most of my assignment operators is
- explicitly invoke the corresponding destructor and copy constructor.
-
- I know how to explicitly call a destructor on an object, but how do
- you explicitly call a copy constructor on what used to be an object?
- The only way I know of to do this is to overload operator new() with a
- pointer argument. The overloaded new() operator would just return
- the pointer. I could then write an assignment operator like the following
- for any class X:
-
- X& X::operator=( const X& from )
- {
- this->~X(); // invoke
- destructor.
- new(this) X(from); // invoke copy
- constructor.
- return( *this );
- }
-
- However overloading new seems a little kludgy when all I want to
- do is call a constructor on a specific object reference.
-
- Please reply via this notes group, I don't currently have access to an
- email server.
-
- Thanks,
- John Matthews
- Hewlett-Packard
-