Hope this is not a duplicate. If so, please point me to it in a comment and I'll remove the question again.
I have a data object with data that's only valid in a bundle - i.e. there's no sense in changing the value of one member without invalidating the other members.
This data object describes some image information:
struct ImageInfo
{
ImageInfo(const double &ppix, ...)
: PpiX(ppix),
...
{ }
const double PpiX;
const double PpiY;
const int SizeX;
const int SizeY;
};
In my image object I have a non-const member of type ImageInfo
:
class MyImageObject
{
...
private:
ImageInfo mMyInfo;
}
I want to be able to change mMyInfo
at runtime, but only so that it will take a new ImageInfo(...) instance.
In the MyImageObject::Load()
function, I'd like to read this data from the file info and then create an ImageInfo
instance with the correct set of data:
double ppix = ImageFile.GetPpiX();
...
mMyInfo = ImageInfo(ppix, ...);
But I couldn't manage to write a valid assignment operator (copy constructor is possible of course). My solution left mMyInfo
empty, because I didn't reference this
:
ImageInfo operator=(const ImageInfo &other)
{
// no reference to *this
return ImageInfo(other);
}
Out of curiosity I'd like to know how the assignment operator for such a class would need to look like.
I'm using plain C++.
EDIT
Possible solutions (the goal is to keep the data transportable, but coherent):
- Use private members together with
Get...()
functions -> simple, but I'd like to avoid the parentheses. - Store a pointer to ImageInfo:
ImageInfo *mpMyInfo;
(I'd like to avoid the heap.) - Use serialization and store the serialized ImageInfo, then create local instances from the serialized data.
const
to begin with? – Raganconst
, they need to bereadonly
. Butreadonly
exists inC#
, but not inC++
. – Brillatsavarinconst
orreadonly
, you obviously want to change these values over time, so why declare that they don't change? – Ragan