|
Prev: debugging a c++ program
Next: map string to function
From: JoeC on 20 Nov 2006 19:40 I have been looking around on how to do a simple assgnment operator, this is the best I can come up with for assigning a simple structure. coord& operator =(const coord& c): x(c.x), y(c.y){return *this;} Thanks.
From: LR on 20 Nov 2006 20:03 JoeC wrote: > I have been looking around on how to do a simple assgnment operator, Where did you look? > this is the best I can come up with for assigning a simple structure. > > coord& operator =(const coord& c): x(c.x), y(c.y){return *this;} I'm pretty sure that doesn't compile. operator= is not a ctor, so the initialization syntax you're using isn't valid here. What book are you using again? Does it have examples of operator=? According to http://www.gotw.ca/gotw/059.htm the canonical form of an assigment operator is copy ctor, swap, return. class MyClass { public: // you must implement these for // the operator= given here to work void swap(MyClass &); MyClass(const MyClass &); // here is the canonical form, according to // gotw #59 MyClass &operator=(const MyClass &m) { MyClass temp(m); swap(temp); return *this; } }; You may also find this of interest: http://www.parashift.com/c++-faq-lite/assignment-operators.html > > Thanks. > You're welcome. LR
From: Mark P on 20 Nov 2006 20:37 JoeC wrote: > I have been looking around on how to do a simple assgnment operator, > this is the best I can come up with for assigning a simple structure. > > coord& operator =(const coord& c): x(c.x), y(c.y){return *this;} > First of all, almost any book on C++ in the universe that isn't completely worthless would give a proper example of an assignment operator. So which worthless books are you using that don't explain this? Second, clear and accurate names are important, both for your benefit and for the benefit of anyone else who might someday need to understand your code (fellow newsgroup readers, perhaps). It looks like you're using "coord" to represent a 2D point. Assuming that coord is short for "coordinate", you should know that a coordinate is conventionally *one* of the component values in the *set* of values which define a location in space. Consider a more appropriate name, such as point or, maybe better still, point2d. Anyway, to get to your actual question, your syntax above is not permitted-- an assignment operator is not a constructor and hence the initializer list after the colon does not belong. You could instead write this function as, coord& operator=( const coord& c) { x = c.x; y = c.y; return *this; } -Mark
From: JoeC on 20 Nov 2006 21:23 Mark P wrote: > JoeC wrote: > > I have been looking around on how to do a simple assgnment operator, > > this is the best I can come up with for assigning a simple structure. > > > > coord& operator =(const coord& c): x(c.x), y(c.y){return *this;} > > > > First of all, almost any book on C++ in the universe that isn't > completely worthless would give a proper example of an assignment > operator. So which worthless books are you using that don't explain this? > > Second, clear and accurate names are important, both for your benefit > and for the benefit of anyone else who might someday need to understand > your code (fellow newsgroup readers, perhaps). It looks like you're > using "coord" to represent a 2D point. Assuming that coord is short for > "coordinate", you should know that a coordinate is conventionally *one* > of the component values in the *set* of values which define a location > in space. Consider a more appropriate name, such as point or, maybe > better still, point2d. > > Anyway, to get to your actual question, your syntax above is not > permitted-- an assignment operator is not a constructor and hence the > initializer list after the colon does not belong. You could instead > write this function as, > > coord& operator=( const coord& c) > { > x = c.x; > y = c.y; > return *this; > } > > > -Mark Thanks.
From: Francis Glassborow on 21 Nov 2006 05:03
In article <GLs8h.8691$6t.5030(a)newssvr11.news.prodigy.com>, Mark P <usenet(a)fall2005REMOVE.fastmailCAPS.fm> writes >First of all, almost any book on C++ in the universe that isn't >completely worthless would give a proper example of an assignment >operator. So which worthless books are you using that don't explain >this? Well they will give you a compilable implementation, with regret the majority will give you a poor version. Some are not safe for self-assignment, many are unsafe in the presence of exceptions. The canonical form given elsewhere in this thread works safely and is a good place to start. -- Francis Glassborow ACCU Author of 'You Can Do It!' and "You Can Program in C++" see http://www.spellen.org/youcandoit For project ideas and contributions: http://www.spellen.org/youcandoit/projects |