Board index » cppbuilder » Re: About operator=

Re: About operator=


2005-10-31 11:13:04 PM
cppbuilder74
tinyabs wrote:
Quote
My condition is that this class is not copyable.
Here's what I do:
class TMyObject
{
// OTHER STUFF
private:
// HIDE COPY CONSTRUCTOR AND ASSIGNMENT OPERATOR
// AND DON'T DEFINE BODIES. WE DON'T PLAN FOR
// INSTANCES OF THIS CLASS TO BE COPIED.
TMyObject(const TMyObject &);
TMyObject & operator=(const TMyObject &);
// OTHER STUFF
};
I'm no language lawyer, but I tested this long ago and it appeared to
be the minimal amount of code that would suffice.
--
Bill D. Pirkle
www.worldchessnetwork.com - Where the world plays chess!
www.tentonhammer.com - EQ2, WoW, GuildWars, Middle-Earth Online, and
more!
 
 

Re:Re: About operator=

Take a look at these prototypes.
class TMyObject
{
protected:
TMyObject(const TMyObject &myObject); // copy ctor
void operator=(const TMyObject &myObject); // non-returning assignment
TMyObject &operator=(const TMyObject &myObject); // returning assignment
};
My condition is that this class is not copyable.
My Question is, do I need to declare the 2 assignemnt operator so that are
in protected scope or just declare anyone will do? My doubt is about which
variant of the operator= was created automatically if I didn't declare them.
Thanks.
 

Re:Re: About operator=

"tinyabs" < XXXX@XXXXX.COM >wrote:
Quote
Take a look at these prototypes.

class TMyObject
{
protected:
TMyObject(const TMyObject &myObject); // copy ctor
void operator=(const TMyObject &myObject); // non-returning assignment
TMyObject &operator=(const TMyObject &myObject); // returning assignment
};

My condition is that this class is not copyable.
Make those private. You don't want those functions being called, so why
let them be any more accessible than they need be.
And a little point. The above shouldn't compile, because
Quote
void operator=(const TMyObject &myObject);
TMyObject &operator=(const TMyObject &myObject);
differ only in their return type, and C++ doesn't allow you to overload
functions only on heir return type.
(A source of occasional annoyance. Ah well.)
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
 

{smallsort}