Jump to content
  • Advertisement
Sign in to follow this  
Kest

Removing =

This topic is 4486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to completely remove or disable the default = operator (focusing on type=type use) without adding a new one for my class types? For example, if I don't want to allow a complex class owning pointers to resources to be copy-able? I would rather certain classes be copied through function calls rather than an operator, because copying will be unusual, there will be alternatives to copying, and I want to avoid accidental copying. Thanks for any information on this.

Share this post


Link to post
Share on other sites
Advertisement
Removing the ability to do assignments in general would be a very bad thing, it sounds like you do just want to do it for a specific class type I think (sorry if I misunderstood). Simply implement an operator= member function for the class you dont want to have the ability and make it private.

Share this post


Link to post
Share on other sites
Aside from adding private: foo & operator=(const foo&); to class foo, you cannot remove the operator. Mind you, making constructors or operators private so they cannot be used is a common idiom in many OO languages.

Share this post


Link to post
Share on other sites
The other cool thing is if you dont define it (after declaring it private), you'll get a linker error if you try to use =

Share this post


Link to post
Share on other sites
So the basic answer is the hack. I figured there would be a better way around that, but I guess I'll take what I can get.

Quote:
Original post by BigBadBob
Removing the ability to do assignments in general would be a very bad thing

Removing the ability to do assignments in general would result in no functional code at all. But I don't plan to write copy functionality for objects that never need to be copied. And there are many, many types of objects in my project that do not need to be copied. Allowing use of the = operator on complex types, without writing copy functionality, means all of those objects just brake down and die violent fish out of the water deaths if the operator is mistakenly used.

Thanks for the help!

Share this post


Link to post
Share on other sites
What the previous posters stated about making it private, and also not defining it, is very good advice.

Infact it is in one of the best C++ books to date.



Another thing you can do, to catch the error before the linker stage, and at compile time,

Just create a base class for the = and/or copy constructor, and make them private there, and then inherit from that class.

That will move the error up to compile time, instead of link time, which is allways good.

Share this post


Link to post
Share on other sites
It's not really a hack. Declaring it private means you get a nice compiler error if outside code tries to use it; not implementing it means you get a linker error (not as nice, but the best you can really do) if inside code tries to use it. It's standard idiom, and if it really galls you, you can make use of boost::noncopyable, which wraps the process.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kest
Removing the ability to do assignments in general would result in no functional code at all.


Ironically, no assignments would result in functional code (as opposed to imperative one).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!