Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>#include &lt;memory&gt; using namespace std; template&lt;class Real, int Size&gt; class TVector { protected: Real *_values; public: TVector() { // allocate buffer _values = new Real[Size]; } TVector(Real *prValues) { // check first if (prValues == 0) throw std::exception("prValues is null"); // allocate buffer _values = new Real[Size]; // initialize buffer with values for (unsigned int i(0U) ; i &lt; Size ; ++i) _values[i] = prValues[i]; } // Do not forget copy ctor TVector(TVector&lt;Real, Size&gt; const &amp;rTVector) { // allocate buffer _values = new Real[Size]; // initialize with other vector *this = rTVector; } virtual ~TVector() { delete [] _values; } virtual Real &amp;operator[](int iIndex) { // check for requested index if (iIndex &lt; 0 || iIndex &gt;= Size) throw std::exception("requested index is out of bounds"); // index is correct. Return value return *(_values+iIndex); } virtual TVector&lt;Real, Size&gt; &amp;operator=(TVector&lt;Real, Size&gt; const &amp;rTVector) { // just copying values for (unsigned int i(0U) ; i &lt; Size ; ++i) _values[i] = rTVector._values[i]; return *this; } virtual TVector&lt;Real, Size&gt; &amp;operator+=(TVector&lt;Real, Size&gt; const &amp;rTVector) { for (unsigned int i(0U) ; i &lt; Size ; ++i) _values[i] += rTVector._values[i]; return *this; } virtual TVector&lt;Real, Size&gt; operator+(TVector&lt;Real, Size&gt; const &amp;rTVector) { TVector&lt;Real, Size&gt; tempVector(this-&gt;_values); tempVector += rTVector; return tempVector; } }; template&lt;class Real&gt; class TVector2: public TVector&lt;Real, 2&gt; { public: TVector2() {}; TVector2(Real *prValues): TVector(prValues) {} TVector2 &amp;operator=(TVector2&lt;Real&gt; const &amp;rTVector) { return static_cast&lt;TVector2 &amp;&gt;(TVector&lt;Real, 2&gt;::operator=(rTVector)); } TVector2 &amp;operator+=(TVector2&lt;Real&gt; const &amp;rTVector) { return static_cast&lt;TVector2 &amp;&gt;(TVector&lt;Real, 2&gt;::operator+=(rTVector)); } TVector2 operator+(TVector2&lt;Real&gt; const &amp;rTVector) { return static_cast&lt;TVector2 &amp;&gt;(TVector&lt;Real, 2&gt;::operator+(rTVector)); } Real &amp;X() { return _values[0]; } Real &amp;Y() { return _values[1]; } }; int main(int argc, char** argv) { TVector2&lt;int&gt; v = TVector2&lt;int&gt;(); v[0]=0; v[1]=1; TVector2&lt;int&gt; v1 = TVector2&lt;int&gt;(); v1.X() = 10; v1.Y() = 15; v = v1; v += v1; v = v + v1; return 0; } </code></pre> <p>Some misc notes:</p> <ol> <li>it's very bad that you use <code>malloc</code> against of <code>new</code>. <code>Real</code> can be POD only to allow <code>vector</code> work well in your case. Use <code>new</code> or provide custom creation policy if you think that <code>malloc</code> provides better performance on PODs. Also do not forget to use <code>delete []</code> instead of <code>free</code> while destroying memory buffer.</li> <li>It's better to perform bounds checking while overloading <code>operator[]</code></li> <li>for better performance use <code>++i</code> instead of postfix form. In former no temporary value is created.</li> </ol>
 

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