From what I understand I’m able to “disable” copying and assigning to my objects by defining private copy constructor and assignment operator:
class MyClass
{
private:
MyClass(const MyClass& srcMyClass);
MyClass& operator=(const MyClass& srcMyClass);
}
But what’s the usage of this?
Is it considered a bad practice?
I would appreciate if you could describe the situation, in which it would be reasonable / useful to “disable” assignment and copy constructor in this way.
It’s useful when it doesn’t make sense for your object to be copied. It is definitely not considered bad practice.
For instance, if you have a class that represents a network connection, it’s not meaningful to copy that object. Another time you may want a class to be noncopyable is if you had a class representing one player in a multiplayer game. Both these classes represent things that can’t be copied in the real world, or that don’t make sense to copy (a person, a connection).
Also, if you are trying to implement a Singleton, it’s standard procedure to make the objects non-copyable.