Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're trying to write an ancient and obscure language called <code>C</code>. Rumor has, with sufficient time and zen meditation, almost all C++ can be processed into this archaic representation. It simply takes time, grasshopper.</p> <p>In all seriousness, the general pattern you're looking for appears to be an interface and implementation.</p> <p>Templates can be avoided in most cases just be copying the code and customizing it by hand, but when you need to handle a situation like this, having a base class that defines all the important stuff is where you typically start.</p> <p>For example:</p> <pre><code>class IAddable { public: virtual IAddable * Add(IAddable * pOther) = 0; }; class Number : public IAddable { public: virtual IAddable * Add(IAddable * pOther) { return new Number(this, pOther); } Number(Number * a, Number * b) : m_Value(a-&gt;m_Value + b-&gt;m_Value) { }; private: int m_Value; }; </code></pre> <p>That's a basic, unsafe example, but it illustrates the point. So yes, your concept is generally sound.</p> <p>As far as using virtual operators, if such a thing is possible (not used them myself), it seems like it would work.</p> <p>Something to consider is making sure the two operands can be operated on, for which you might want to define a type check in your interface (similar to COM's <code>IUnknown::QueryInterface</code>);</p> <p>You also have to watch how you handle the inheriting types; a lot of pointers will be involved since neither you nor the compiler may know quite what they are. Make sure you check for errors and wrong types rather often (return error values or convert the types or throw or what-have-you).</p>
    singulars
    1. This table or related slice is empty.
    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.
    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