Return Valule Optimization in C++11

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3778
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: Return Valule Optimization in C++11

Post by Ovidiu Cucu » 31 Oct 2013, 18:51

Astea fiind zise, poti face fara frica de overhead, ceva de genul

Code: Select all

class PointD
{
    double m_x;
    double m_y;
public:
    PointD() : m_x(0), m_y(0) {}
    PointD(double x, double y) : m_x(x), m_y(y) {}
    PointD operator+(const PointD& point) const
    {
        return PointD(m_x + point.m_x, m_y + point.m_y);
    }
    // etc.
};
Sau, un pic mai jmeker, ca sa nu scrii acelasi cod pentru N tipuri, cu template:

Code: Select all

template <class T> class PointT
{
    T m_x;
    T m_y;
public:
    PointT() : m_x(0), m_y(0) {}
    PointT(T x, T y) : m_x(x), m_y(y) {}
    PointT operator+(const PointT& point) const
    {
        return PointT(m_x + point.m_x, m_y + point.m_y);
    }
    // etc.
};

typedef PointT<double> PointD;
typedef PointT<int> PointI;
// etc.



viorel2005
Membru
Membru
Posts: 208
Joined: 24 May 2008, 09:41

Re: Return Valule Optimization in C++11

Post by viorel2005 » 01 Nov 2013, 08:34

Multumesc pentru raspunsuri.

Post Reply