Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I assign a data object with const members?
    primarykey
    data
    text
    <p>Hope this is not a duplicate. If so, please point me to it in a comment and I'll remove the question again.</p> <p>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.<br> This data object describes some image information:</p> <pre><code>struct ImageInfo { ImageInfo(const double &amp;ppix, ...) : PpiX(ppix), ... { } const double PpiX; const double PpiY; const int SizeX; const int SizeY; }; </code></pre> <p>In my image object I have a <em>non-const</em> member of type <code>ImageInfo</code>:</p> <pre><code>class MyImageObject { ... private: ImageInfo mMyInfo; } </code></pre> <p>I want to be able to change <code>mMyInfo</code> at runtime, but only so that it will take a new ImageInfo(...) instance.</p> <p>In the <code>MyImageObject::Load()</code> function, I'd like to read this data from the file info and then create an <code>ImageInfo</code> instance with the correct set of data:</p> <pre><code>double ppix = ImageFile.GetPpiX(); ... mMyInfo = ImageInfo(ppix, ...); </code></pre> <p>But I couldn't manage to write a valid assignment operator (copy constructor is possible of course). My solution left <code>mMyInfo</code> empty, because I didn't reference <code>this</code>:</p> <pre><code>ImageInfo operator=(const ImageInfo &amp;other) { // no reference to *this return ImageInfo(other); } </code></pre> <p>Out of curiosity I'd like to know how the assignment operator for such a class would need to look like.</p> <p>I'm using plain C++.</p> <p><strong>EDIT</strong><br> Possible solutions (the goal is to keep the data transportable, but coherent):</p> <ul> <li>Use private members together with <code>Get...()</code> functions -> simple, but I'd like to avoid the parentheses.</li> <li>Store a pointer to ImageInfo: <code>ImageInfo *mpMyInfo;</code> (I'd like to avoid the heap.)</li> <li>Use serialization and store the serialized ImageInfo, then create local instances from the serialized data.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload