<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to implement a moveable type with MAXON_OPERATOR_MOVE_ASSIGNMENT?]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<hr />
<p dir="auto">edit <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/ferdinand">@<bdi>ferdinand</bdi></a>:</p>
<p dir="auto">How to implement a class that can be stored in a <code>BaseArray</code> that has virtual member functions where a class instance is not copyable but moveable?</p>
<p dir="auto">This is specifically about using the macro <code>MAXON_OPERATOR_MOVE_ASSIGNMENT</code>.</p>
<hr />
<p dir="auto">let's say, I have a class like this, with a virtual function:</p>
<pre><code class="language-cpp">class SomeClass
{
    MAXON_DISALLOW_COPY_AND_ASSIGN(SomeClass);

public:
    virtual maxon::Result&lt;void&gt; PopulateArray()
    {
        // Fill _vectorArray with something
        return maxon::OK;
    }

    virtual maxon::Result&lt;void&gt; CopyFrom(const SomeClass&amp; src)
    {
        return _vectorArray.CopyFrom(src._vectorArray);
    }

private:
    maxon::BaseArray&lt;maxon::Vector&gt; _vectorArray;

public:
    SomeClass()
    {}

    virtual ~SomeClass()
    {}

    SomeClass(SomeClass&amp;&amp; src)
    {
        // What to do with _vectorArray?
    }
    MAXON_OPERATOR_MOVE_ASSIGNMENT(SomeClass);
};
</code></pre>
<p dir="auto">Since the class contains a <code>maxon::BaseArray</code>, I decorated it with a <code>MAXON_DISALLOW_COPY_AND_ASSIGN()</code> statement. Therefore, to use it in a <code>BaseArray</code>, I have to implement a <code>CopyFrom()</code> function and a move assignment operator. I also added a virtual destructor to avoid compiler error C4265.</p>
<p dir="auto">Let's test it</p>
<pre><code class="language-cpp">SomeClass testObject;
maxon::BaseArray&lt;SomeClass&gt; testArray;
testArray.Append(testObject) iferr_return;
</code></pre>
<p dir="auto">I get the error:<br />
<code>C2338: MAXON_OPERATOR_MOVE_ASSIGNMENT can't be used for classes with virtual functions.</code></p>
<p dir="auto">So, long story short: How do I create arrays of classes that don't allow copy and assign, and also can't use <code>MAXON_OPERATOR_MOVE_ASSIGNMENT</code> because they have virtual functions? Does that even work at all? And would it work with classes derived from <code>SomeClass</code>?</p>
<p dir="auto">Cheers,<br />
Frank</p>
<p dir="auto">edit <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/fwilleke80">@<bdi>fwilleke80</bdi></a>:<br />
I guess, the easiest solution is to not store SomeClass objects in a BaseArray, but to store SomeClass* pointers in a PointerArray, right?</p>
]]></description><link>http://developers.maxon.net/forum/topic/14738/how-to-implement-a-moveable-type-with-maxon_operator_move_assignment</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 07:41:42 GMT</lastBuildDate><atom:link href="http://developers.maxon.net/forum/topic/14738.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 08 Jun 2023 12:33:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to implement a moveable type with MAXON_OPERATOR_MOVE_ASSIGNMENT? on Thu, 17 Aug 2023 06:56:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/jana">@<bdi>jana</bdi></a> sorry, I forgot to flag it myself. All solved, thank you <img src="http://developers.maxon.net/forum/assets/plugins/nodebb-plugin-emoji/emoji/android/1f44d.png?v=0b8ddba251d" class="not-responsive emoji emoji-android emoji--+1" style="height:23px;width:auto;vertical-align:middle" title=":+1:" alt="👍" /></p>
]]></description><link>http://developers.maxon.net/forum/post/72263</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/72263</guid><dc:creator><![CDATA[fwilleke80]]></dc:creator><pubDate>Thu, 17 Aug 2023 06:56:58 GMT</pubDate></item><item><title><![CDATA[Reply to How to implement a moveable type with MAXON_OPERATOR_MOVE_ASSIGNMENT? on Thu, 10 Aug 2023 05:45:52 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/fwilleke80">@<bdi>fwilleke80</bdi></a> ,</p>
<p dir="auto">without further questions or postings, we will consider this topic as solved by Friday, the 11th of august 2023 and flag it accordingly.</p>
<p dir="auto">Thank you for your understanding,<br />
Maxon SDK Group</p>
]]></description><link>http://developers.maxon.net/forum/post/72214</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/72214</guid><dc:creator><![CDATA[jana]]></dc:creator><pubDate>Thu, 10 Aug 2023 05:45:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to implement a moveable type with MAXON_OPERATOR_MOVE_ASSIGNMENT? on Fri, 09 Jun 2023 10:08:27 GMT]]></title><description><![CDATA[<p dir="auto">Thank you, Ferdinand!<br />
That really does help a lot <img src="http://developers.maxon.net/forum/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=0b8ddba251d" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:23px;width:auto;vertical-align:middle" title=":-)" alt="🙂" /> <img src="http://developers.maxon.net/forum/assets/plugins/nodebb-plugin-emoji/emoji/android/1f44d.png?v=0b8ddba251d" class="not-responsive emoji emoji-android emoji--+1" style="height:23px;width:auto;vertical-align:middle" title=":+1:" alt="👍" /></p>
<p dir="auto">Have a nice weekend!</p>
]]></description><link>http://developers.maxon.net/forum/post/71702</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/71702</guid><dc:creator><![CDATA[fwilleke80]]></dc:creator><pubDate>Fri, 09 Jun 2023 10:08:27 GMT</pubDate></item><item><title><![CDATA[Reply to How to implement a moveable type with MAXON_OPERATOR_MOVE_ASSIGNMENT? on Fri, 09 Jun 2023 14:19:30 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/fwilleke80">@<bdi>fwilleke80</bdi></a>,</p>
<p dir="auto">Thank you for reaching out to us. Please remember to consolidate your postings for the initial question of a topic, so that it remains compact. Please also remember to put your question at the beginning of a topic, ideally into the first sentence and not to the end of it. Finally, it is often best to describe what you want to achieve, which has been done here at best implicitly. I have fixed these points here, but please follow them in future threads.</p>
<p dir="auto">In short, you cannot have classes with virtual member functions that use the macro <code>MAXON_OPERATOR_MOVE_ASSIGNMENT</code> as stated in its <a href="https://developers.maxon.net/docs/cpp/2023_2/group___s_y_s_t_e_m.html#gacd702e11a4c9c1b4908f0e586e6844b8" target="_blank" rel="noopener noreferrer nofollow ugc">documentation</a>:</p>
<blockquote>
<p dir="auto">This macro can't be used for classes with virtual functions for the sake of safety. Because the created move assignment operator would invoke the constructor, the vtable pointer of the object would be changed if one accidentally used the assignment operator of a base class for an object of a derived class.</p>
<p dir="auto">This macro must not be used for classes which support concurrent move assignment calls from multiple threads to the same object because the sequence of destructor and move constructor is not thread safe.</p>
</blockquote>
<p dir="auto">You could of course implement the move-operator yourself (you should obey the <a href="https://en.cppreference.com/w/cpp/language/rule_of_three" target="_blank" rel="noopener noreferrer nofollow ugc">three/five/zero rule</a> then). But such implementation would then also have exactly that problem the docs talk about. For your case, it could look something like shown in [1]. You could also specialize the relevant operators and constructors for the derived classes, but that could be quite a bit of work, depending on how many there are.</p>
<p dir="auto">I personally would not bother with all this and simply would not store the objects in the collection.  The type <a href="https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_pointer_array.html" target="_blank" rel="noopener noreferrer nofollow ugc">maxon::PointerArray</a> you mentioned is one option, although it also takes over the memory management of the "stored" objects for you, so in the end it sort of works like a <code>BaseArray</code>.</p>
<p dir="auto">Depending on what you are trying to do, a <code>WeakRawPtr</code> or a maxon reference based approach might be better. [2][3]</p>
<p dir="auto">Cheers,<br />
Ferdinand</p>
<p dir="auto">[1]</p>
<pre><code class="language-cpp">  // The move assignment operator, we just move the internal data which must be moved. But it will not rectify
  // the general problems described in the docs.
  //
  // We should also implement the copy and move constructors.
  SomeClass&amp; operator =(SomeClass&amp;&amp; src)
  {
    _vectorArray = std::move(src._vectorArray);
    return *this;
  }
</code></pre>
<p dir="auto">[2] <a href="https://developers.maxon.net/docs/cpp/2023_2/page_handbook_weakrawptr.html" target="_blank" rel="noopener noreferrer nofollow ugc">WeakRawPtr Manual</a>: No memory management, but guards against access violations for deleted objects.</p>
<pre><code class="language-cpp">maxon::BaseArray&lt;maxon::WeakRawPtr&lt;SomeClass&gt;&gt; _data;
</code></pre>
<p dir="auto">[3] <a href="https://developers.maxon.net/docs/cpp/2023_2/page_maxonapi_references.html" target="_blank" rel="noopener noreferrer nofollow ugc">References Manual</a>: Varying degrees of memory management, up to the degree of automated deallocation over ref counting. E.g.,  a copy-on-write ref, the fanciest type of refs:</p>
<pre><code class="language-cpp">maxon::BaseArray&lt;maxon::StrongCOWRef&lt;SomeClass&gt;&gt; _data;
</code></pre>
]]></description><link>http://developers.maxon.net/forum/post/71700</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/71700</guid><dc:creator><![CDATA[ferdinand]]></dc:creator><pubDate>Fri, 09 Jun 2023 14:19:30 GMT</pubDate></item></channel></rss>