I have a simple struct to hold two const doubles.
struct Scale { const double SCALE_X, SCALE_Y; Scale(double, double); }; Scale::Scale(double sx, double sy) : SCALE_X(sx), SCALE_Y(sy) { } I have this function that returns a boolean as confirmation, and sets the passed oscale pointer to whatever calculated value found inside.
bool FindSequence(Scale* oscale) { // ... *oscale = Scale(sx, sy); // ... return true; } In use:
Scale *scale; if (FindSequence(scale)) { std::cout << "Yes" << std::endl; } Compile log:
photon.cpp:286:14: error: use of deleted function 'photon::Scale& photon::Scale::operator=(photon::Scale&&)' *oscale = Scale(sx, sy); ^ In file included from photon.h:5:0, from photon.cpp:4: globals.h:76:9: note: 'photon::Scale& photon::Scale::operator=(photon::Scale&&)' is implicitly deleted because the default definition would be ill-formed: struct Scale { ^ globals.h:76:9: error: non-static const member 'const double photon::Scale::SCALE_X', can't use default assignment operator globals.h:76:9: error: non-static const member 'const double photon::Scale::SCALE_Y', can't use default assignment operator Do I have to override the operator=? Is it because the Scale struct is immutable? What am I doing wrong?
const-qualified objects or references. And whatever you try to get around it will cause UB (aside from removing the qualifiers).Scale *scale; if (FindSequence(scale)) {would invoke UB if you dereference the pointer, as you do in*oscale = Scale(sx, sy);. (Dereferencing an uninitialized pointer is UB.) You mean this:Scale scale; if (FindSequence(&scale)) {. But what you probably really mean isbool FindSequence(Scale & oscale)andScale scale; if (FindSequence(scale)) {.SCALE_XandSCALE_Ymembers non-const. If you have aScaleobject then you can change them, but if you have aconst Scaleobject then you can't. Forcing them read-only is bad practice; if the consumer wants a read-onlyScaleobject then they can just declare itconst.Scale scale;does not throw an error or is there another way around not having to actually create the object?Scalethrow an error? Unless it is doing something we don't know about. Maybe you should post the defaultScaleconstructor.