Can I prevent object assignment? - c++

I want to ensure that the following type of call is illegal:
MyClass me;
MyClass you;
me = you; // how to make this illegal?
Is it possible?

Declare the assignment operator private:
class A {
private:
void operator=(const A&);
...
};
but don't provide an implementation - you will get a compile or link-time error if you try to perform an assignment to A.
I prefer to use a macro to do this. This also prevents copying, by making the copy constructor private too:
#define CANNOT_COPY( class ) \
private: \
class(const class&); \
void operator=(const class &) \
I can then say things like:
class A {
CANNOT_COPY( A );
...
};
which is easy to read and easy to search for.

declare the assignment operator as private.

Yes - define a private assignment operator (operator=) or derive from the handy boost::noncopyable class.

As of C++11, my understanding is that the preferred solution is to use the '= delete' construct:
class MyClass {
MyClass (const MyClass&) = delete; // Disable copy constructor
MyClass& operator=(const MyClass&) = delete; // Disable assignment
...
}
Then
MyClass me;
MyClass you;
me = you; // won't compile

Use const.
MyClass const me;
MyClass you;
me = you; // compilation error

I use to derive my noncopyable classes from a common noncopyable class. If not using boost, I usually use this shortcut:
class NonCopyable
{
protected:
/**
* Protected default constructor
*/
NonCopyable() {}
/**
* Protected destructor
*/
~NonCopyable() {}
private:
/**
* Private copy constructor
*/
NonCopyable( const NonCopyable& );
/**
* Private assignment operator
*/
const NonCopyable& operator=( const NonCopyable& );
};
Note that neither the copy constructor nor the assignment operators have an implementation.

The other answers here are all correct. However, it is important to note that you are only prohibiting the specific code you mentioned. Someone can still come along and make a duplicate of your class by using memcpy or other strategies.

Related

How to delete all implicit (default) methods of class in C++?

C++ compiler in certain conditions for class adds implicit default constructor, copy constructor, destructor, conversion operators, assignment operators, etc. And other implicit methods.
How do I disable/delete all possible implicit methods in shortest way possible?
I don't want my class to be un-copyable, I want just these default methods to be disabled so that compiler throws compile error and I implement them by myself. I just want to have over-control, so that compiler doesn't do any silent work about my class.
For example I can do next thing (this is just a dummy example, in real life I can have any complex class):
class C {
private:
C() = delete;
C(C const & other) = delete;
C & operator = (C const & other) = delete;
};
But this is long way, also I can forget deleting some of methods signatures and I need to delete all of them. So that I reimplement all methods from scratch by myself.
And I need to see errors for all methods that other code needs/uses and that I have not yet implemented, instead of compiler silently implementing those methods for me.
Also if I delete default methods like I did above in my example, how can I be sure that there are no other silently created methods? Is there online a list of 100% all standard methods created silently by compiler?
You may go for a mixin approach. Define the cumbersome class once
class DeleteAllSpecialMemebers {
private:
DeleteAllSpecialMemebers() = delete;
DeleteAllSpecialMemebers(DeleteAllSpecialMemebers const &) = delete;
DeleteAllSpecialMemebers & operator = (DeleteAllSpecialMemebers const &) = delete;
DeleteAllSpecialMemebers(DeleteAllSpecialMemebers &&) = delete;
DeleteAllSpecialMemebers & operator = (DeleteAllSpecialMemebers &&) = delete;
protected:
struct token { explicit token() = default; };
DeleteAllSpecialMemebers(token) {}
};
Then inherit from it:
class C : private DeleteAllSpecialMemebers{
public:
C(int) : DeleteAllSpecialMemebers(token{}) {}
};
The extra token type is to allow deriving classes to implement other constructors, such as C::C(int) (or even a user-provided C::C()).
See it live.
The shortest option might be to have a reusable dummy base class:
struct NoSpecialMember {
NoSpecialMember(const NoSpecialMember&) = delete;
NoSpecialMember& operator=(const NoSpecialMember&) = delete;
NoSpecialMember(NoSpecialMember&&) = delete;
NoSpecialMember& operator=(NoSpecialMember&&) = delete;
~NoSpecialMember() = default;
};
It's sufficient to inherit privately;
class Test : private NoSpecialMember {};
with the following result:
static_assert(!std::is_copy_constructible_v<Test>);
static_assert(!std::is_copy_assignable_v<Test>);
static_assert(!std::is_move_constructible_v<Test>);
static_assert(!std::is_move_assignable_v<Test>);
Define helper and use it as field of class where it should be disabled:
struct DisableCopyAssign
{
DisableCopyAssign(char){}
DisableCopyAssign(const DisableCopyAssign&) = delete;
DisableCopyAssign(DisableCopyAssign&&) = delete;
};
class Foo
{
private:
DisableCopyAssign dummy;
};
https://gcc.godbolt.org/z/ce8sjq

Can changing from private constructor/assignment operator to deleted break binary compatibility?

Using C++11.
I have a class I want to clean-up a bit by making the following changes:
From
class MyClass {
public:
// code
private:
MyClass(const MyClass&);
MyClass& operator=(const MyClass&);
// members
};
To
class MyClass {
public:
// code
MyClass(const MyClass&) = delete;
MyClass& operator=(const MyClass&) = delete;
private:
// members
};
Knowing that both are declared but not defined, will this change break binary compatibility?
Does it improves anything?
If you switch from your first version to the second, where you have accessible, user-declared, deleted constructors, code like this will compile in C++11:
MyClass b{};
But if you upgrade to C++20, it won't. That might not be something you want. If you stick with your first version, where the constructors are inaccessible, the declaration of b won't compile in any language version, so you won't have this problem at least.
Here's a demo.

Most concise way to disable copying class in C++11

I have a problem dealing with deprecated since C++11 default generation of copy constructor and copy assignment operator when there is a user-defined destructor.
For most sufficiently simple classes default-generated constructors, operators and destructor are fine. Consider the following reasons to declare destructor:
Making trivial destructor virtual in base class:
// header
class Base1 { public: virtual ~Base1() = default; };
class Base2 { public: virtual ~Base2(); };
// source
Base2::~Base2() = default;
Would all 4 copy and move special methods be generated by compiler in these cases? If yes, then I think it is fine and there is no need to complicate Base1 or Base2.
Printing debug message in destructor:
// header
class D { public: ~D(); };
// source
D::~D() {
#ifdef DEBUG_THIS
std::cout << "D was destructed." << std::endl;
#endif
}
I believe that in this case copy constructor and assignment operator would be generated; but move constructor and assignment operator would not. I want to avoid using deprecated default-generating and disable copying of D. I also want to avoid flooding D with 4 deleted declarations. Is disabling only one copy constructor enough? Is it a good style?
With C++11, a clean way is to follow the pattern used in boost (see here)
You basically create a base class where copy constructor and copy assignment are deleted, and inherit it:
class non_copyable
{
protected:
non_copyable() = default;
~non_copyable() = default;
non_copyable(non_copyable const &) = delete;
void operator=(non_copyable const &x) = delete;
};
class MyClass: public non_copyable
{
...
}
Deleting the copy-constructor and copy-assignment operator is the simplest and clearest way to disable copying:
class X
{
X(X const &) = delete;
void operator=(X const &x) = delete;
};
I don't follow what you are talking about with virtual destructors in the question body . It sounds like you're asking for a way to make your code take up fewer characters of source code, but also be more cryptic to anybody looking at it.
If the list of deleted functions bothers you, you could hide them behind a macro, I guess.
#define NON_COPYABLE_NOR_MOVABLE(T) \
T(T const &) = delete; \
void operator=(T const &t) = delete; \
T(T &&) = delete;
Only copy constructor and copy assignment operator will be generated when destructor is explicitly defaulted. And even then their generation is deprecated. So, in order to have virtual destructor and all default methods, one should write the following:
struct Base
{
Base()=default;
virtual ~Base() = default;
Base(const Base&)=default;
Base& operator=(const Base&)=default;
Base(Base&&)=default;
Base& operator=(Base&&)=default;
};
I would definitely use a macro for more than one such Base class.
In case when destructor is defined by user, 2 special methods are still generated. There are the following ways to disable deprecated generating copy constructor and copy assignment operator:
delete move constructor OR move assignment operator (not quite self-explanatory but very short):
Base(Base&&)=delete; // shorter than deleting assignment operator
delete both copy constructor and copy assignment operator:
Base(const Base&)=delete;
Base& operator=(const Base&)=delete;
Note that you have to explicitly declare default constructor if you need it, e.g. Base()=default;.
Macro or inheriting special class can be used as well for this purpose but I personally prefer deleting move constructor to implementing my own macro or base class. When using Qt or boost, I would prefer Q_DISABLE_COPY(Base) and inheriting boost::noncopyable respectively, because they are already implemented, widely known and recognizable.
http://accu.org/index.php/journals/1896 - detailed explanation and rationale for these issues.
You can do it by this(which is used by Caffe: a fast open framework for deep learning):
// Disable the copy and assignment operator for a class.
#define DISABLE_COPY_AND_ASSIGN(classname) \
private:\
classname(const classname&);\
classname& operator=(const classname&)
Usage example:
class CNoCopyable{
public:
CNoCopyable(int i):m_d(i){}
private:
int m_d;
// add this line(pass class name)
DISABLE_COPY_AND_ASSIGN(CNoCopyable);
};

Creating a derived class from a base

I get the following error message:
conversion from ‘BaseClass’ to non-scalar type ‘DerivedClass’ requested
while compiling something like:
AnotherClass response;
DerivedClass message = response.serialize(QString("someStuff"));
where I have
BaseClass AnotherClass::serialize( const QString& valueName ) const
and
class DerivedClass : public BaseClass
{
...
DerivedClass &operator=(const BaseClass &other);
}
Can someone explain what is wrong here, and more importantly why?
DerivedClass message = response.serialize(QString("someStuff"));
This is copy-initialization, not assignment, and operator= doesn't even come in play.
You either need a constructor that takes BaseClass or write it like this:
AnotherClass response;
DerivedClass message;
message = response.serialize(QString("someStuff")); // assignment
Besides using copy-initialization, not assignment as you think you are, you are also violating the rule of three/four.
The rule is that if you have any of the below, you must have all of them (either implemented or deleted using Q_DECL_EQ_DELETE):
Destructor
Copy Constructor
Move Constructor (for C++11)
Assignment Operator
For example, suppose you don't want to implement assignment. You should have:
class MyClass {
// private, deleted - compatible with both C++11 and C++98/03.
MyClass & operator=(const MyClass &) Q_DECL_EQ_DELETE;
public:
MyClass(const MyClass &);
MyClass(MyClass &&);
virtual ~MyClass();
...
};

Not copyable object and exception in copy constructor and operator=

I'd like to make object of class not copyable so I put copy constructor and operator= in private section. However one class is friend of this class so it has access to private methods. Is it good idea to put throw exception in copy constructor and operator= to be sure that object will not be copied?
One approach to make it not copyable is just to declare the copy constructor, but don't implement it at all. That will force a linker error at compile time if anyone tries to use it.
class foo
{
private:
foo(const foo&); // not defined
foo& operator=(const foo&); // not defined
};
#Mysticial have answered this question which is usually done in C++03. But in C++11, you can do this, more nicely:
class foo
{
private:
foo(const foo&) = delete;
foo& operator=(const foo&) = delete;
};
The =delete conveys the message that foo doesn't support copy-semantic, as it has been disabled by explicitly marking it with delete. I've explained this in detail here:
Is there a way to disable constructor synthesizing on a class?