0

Coplien's form tells you to overload the operator =. It's easy when your class has no const attributes but when it does it's more complicated.

class MyClass {
    public:
        MyClass( MyClass const & src );
        MyClass( void );
        MyClass( name, age, leggedness );
        ~MyClass( void );

        // Member functions etc...
    private:
        std::string const _name;
        int const         _number_of_leg;
        int               _age;
        // other const and non-const attributes...
}

What is the best way of overloading the operator = for that kind of class ? Both keeping and not keeping const attributes values from source.

Soocks
  • 139
  • 10
  • 1
    There is no "best" way because a priori it makes no sense to mutate const objects. It will very much depend on context whether there is an `operator=` that makes sense for a particular class with const members. – juanchopanza Apr 02 '18 at 09:17
  • What problem are you encountering? Post a [MCVE] that shows your intent. No ellipses, please. As is, the question is unanswerable. (What does the default constructor set 'age' to, and why?) In general, the best practice for copy-constructors, assignment-operators, and all that is to use the "copy-and-swap" pattern. https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom – Jive Dadson Apr 02 '18 at 09:19
  • 1
    This IS a perfectly valid question with the potential for interesting answers that could go *very* deep. – Johan Lundberg Apr 02 '18 at 09:29
  • 1
    @JohanLundberg Valid, yes. Or at least perhaps. But it is also very broad. SO has a policy against broad questions, and questions that are primarily opinion-based. They lead to never-ending discussions, not definitive answers. – Jive Dadson Apr 02 '18 at 09:33
  • I was unfamiliar with the term Coplien's form. I googled. Turns out C's f has only two constructors, namely a default constructor and a copy constructor. How could such a thing have a meaningful const component like `_age` that is not the same for every object? – Jive Dadson Apr 02 '18 at 09:36
  • @JohanLundberg A virtual +1 however for being a particle physicist. Respect. – Jive Dadson Apr 02 '18 at 09:44
  • Coplien's form must contains at least two constructor : default and copy. But not only 2 ! – Soocks Apr 02 '18 at 09:45
  • All I know is what I read on the internet. Regardless of what the name means, can you answer the questions? Like what does the default constructor set `_age` to? And how does that change if the default is not correct? Oh wait. I see you moved the goal posts. `_age` is no longer const. Now we're getting somewhere. – Jive Dadson Apr 02 '18 at 09:54
  • Well a `const` age would be great but it's not really realistic right ? I changed the example but that does not change the question, it's just an example – Soocks Apr 02 '18 at 10:01
  • Now the question becomes, what to do about a uni-dexters? https://www.youtube.com/watch?v=lbnkY1tBvMU – Jive Dadson Apr 02 '18 at 10:02
  • I thought the question was simple... I thing they have one legs. – Soocks Apr 02 '18 at 10:04
  • And how is Coplien's defautl constructor supposed to infer leggedness? With this, I officially give up. – Jive Dadson Apr 02 '18 at 10:09
  • You're not helping at all, it's a sample class and we absolutely don't care about what it contains. – Soocks Apr 04 '18 at 13:34

1 Answers1

0

It depends on the meaning of the members and of the assignment operators.

Why is your member declared as const in the first place?

If all classes should have the same value, making it a static const would be a better approach (you use less memory, since there would be only one copy of it).

If it simply should not change, this includes that it should not change by assigning to other object. So the assignment operator should not touch them. But this wouldn't create a perfect copy of your object, which might not be what you want at best and confusing at worst.

In this case, one option would be to forbid assignment.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • Is "The question cannot be answered" an answer? The OP needs to clarify the question. – Jive Dadson Apr 02 '18 at 09:24
  • Here, I use `const` to say : Don't change it after construction. But if I totally reassign the variable it has no sense for me to NOT change the `const` also. What I understand : `foo = bar;` then `foo == bar` should be `true` even with `const` attributes – Soocks Apr 02 '18 at 09:49
  • Then how about make it a non-const private and do not provide any means to change it in the public interface? For all practical purposes, it is a constant to the user. The only possible downside is that you have to take care when writing the implementation to enforce this. – Paul92 Apr 02 '18 at 09:52
  • It's strange to "emulate" the const behavior and not really safe and hard to defend in front of the teacher... – Soocks Apr 02 '18 at 09:59
  • Actually you don't answer the question. Read it again – Soocks Apr 04 '18 at 13:32
  • Having a non-const variable private is not "emulating" constness. It is constant - in a broader sense that the one the language allows you to specify. In some places in literature, this is reffered to as "logical constness" as opposed to "bitwise constness". And I don't answer your question directly because what you are trying to achieve is impossible - I just shown ways to achieve the same result and questions to ask on whether that is what you really want. – Paul92 Apr 04 '18 at 14:19