4

I've had it drilled into my head many many times that if a copy-constructor is provided, an assignment operator must also be provided. However, there are times when a class can use a copy-constructor but not an assignment operator.

For example:

class A {
public:
  const int myVar;

  A(const int var) : myVar(var) {};
  A(const A& other) : myVar(other.myVar) {};
};

So is this a terrible thing to do? Does the assignment operator need to be defined but made private? Is such a class still copy-constructable?

2
  • This class would not even need an explicit copy constructor, since the compiler generated one does exactly what you want. Commented Jan 17, 2012 at 19:03
  • 1
    @R.MartinhoFernandes: I am assuming this is a simplistic code sample provided to express the problem. Commented Jan 17, 2012 at 19:05

1 Answer 1

4

So is this a terrible thing to do?
No, it is not.
Not all classes need to be copy constructible as well as assignable. It is perfectly valid to have copy constructible yet non assignable classes.

Is such a class still copy-constructable?
Yes it is.
As long as your class provides a public copy constructor, Your class is copy constructible.

Does the assignment operator need to be defined but made private?
It depends on your usage.
If your class needs to be assignable then it should ideally not have a const member.

The default compiler generated copy assignment operator will not work if your class has an const member because it tries to assign to a const member which is not allowed. So if your code needs a copy assignment operator you will have to provide your own overloaded version. But, Anyway this overloaded version cannot provide expected assignment semantics.

If your class objects do not need to be Assignable then do not define it. If your code does accidentally uses it the compiler will generate an error anyways.

Sign up to request clarification or add additional context in comments.

4 Comments

It does not need to be assignable, but it does need to be copy-constructable. But I'm used to always being told if it has a copy-constructor it also must have an assignment operator, but that doesn't make sense here.
@tpg2114: It is the Rule of Three in C++03 or the Rule of Five in C++11 you refer to here.But it is a guideline true in most cases.It is not an condition enforced by the standard(though it would help if the standard did so).Classes can be copy-construable but not Assignable and it is okay to break the guideline in suce a case.
The rule of three/five permits to declare some of the required items private, and leave them unimplemented. But they should be declared. You declare them because you do not want the compiler to generate something unexpected for you, not because you plan to actually use them
@n.m.: In this particular case,OP does not need to provide any overloading declaration,If OPs code in some strange obscure way uses assignment the implicit copy assignment operator will be called and it will issue a compiler error(since the const member).It will achieve what one would achieve by not providing definition(but only declaration) of the overloaded operator,advantage in this case is that the error will be detected at compile time rather than at linking phase as in the later case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.