THE BIG THREE

June 2009
Roman Okolovich
Example
class CDummy
{
public:
  CDummy()
   {m_intCount++; cout << "create" << endl;}
  ~CDummy()
   {m_intCount--; cout << "destroy" << endl;}

     static int m_intCount;
};

int CDummy::m_intCount = 0;

void foo(CDummy object)
{ /* Argument passed by value */ }

int main(void)                                          Result:
{
  CDummy obj;                                           Create
  cout << "Count = " << obj.m_intCount << endl;          cnt = 1
  foo(obj);                                             destroy
  cout << "Count = " << obj.m_intCount << endl;          cnt = 0
  foo(obj);                                             destroy
  cout << "Count = " << obj.m_intCount << endl;          cnt = -1
  return 0;                                             destroy
}

     08.02.2010                         The Big Three               2
Special member functions
   Special member functions in C++ are functions which the compiler will
    automatically generate if they are used, but not declared explicitly by the
    programmer. The special member functions are:
       Default constructor (if no other constructor is explicitly declared)
       Copy constructor
       Copy assignment operator
       Destructor
   The rule of three (also known as the Law of The Big Three or The Big Three) is a
    rule of thumb in C++ that claims that if a class defines one of the following it
    should probably explicitly define all three:
       Copy constructor
       Copy assignment operator
       Destructor
class CDefault
{
 public:
  CDefault();                                                //   default constructor
  CDefault(const CDefault& object);                          //   copy constructor
  virtual ~CDefault();                                       //   default destructor
  Cdefault& operator = (const CDefault& object);             //   copy assignment operator
};

    08.02.2010                               The Big Three                                   3
Overloaded member function
class CDummy
{
  public:
   CDummy(int p) : m_intValue(p) { cout << "create" << m_intValue << endl; }
   virtual ~CDummy()             { cout << "destroy" << endl; }

     CDummy(const CDummy& object) {m_intValue = object.m_intValue;}

     // polymorphic functions (different data types)
     void overloaded(void)       { cout << "overload "       << ++m_intValue << endl; }
     void overloaded(void) const { cout << "overload const " << m_intValue << endl; }

     int m_intValue;
};

int main(void)
{
  CDummy const dd(2);
  dd.overloaded();
  CDummy bb(3);
  bb.overloaded();

    Cdummy const obj(bb); // copy constructor can be without const
    Cdummy cobj(obj);     // copy constructor must be with const
}


     08.02.2010                           The Big Three                                   4
Default class declaration
class CDefault
{
public:
 // default constructor
 CDefault();
 // copy constructor
 CDefault(const CDefault& object);
 // default destructor
 ~CDefault();

 // copy assignment operator
 CDefault& operator = (const CDefault& object);

 // Optional
 // Note: overloaded operator should be overloaded with const also

 CDefault&         operator*();
 CDefault const&   operator*() const;
 CDefault*         operator->();
 CDefault const*   operator->() const;
};



  08.02.2010                         The Big Three                   5
References
 Special member functions
 Assignment operator in C++
 Type polymorphism




    08.02.2010        The Big Three   6

The Big Three

  • 1.
    THE BIG THREE June2009 Roman Okolovich
  • 2.
    Example class CDummy { public: CDummy() {m_intCount++; cout << "create" << endl;} ~CDummy() {m_intCount--; cout << "destroy" << endl;} static int m_intCount; }; int CDummy::m_intCount = 0; void foo(CDummy object) { /* Argument passed by value */ } int main(void) Result: { CDummy obj; Create cout << "Count = " << obj.m_intCount << endl; cnt = 1 foo(obj); destroy cout << "Count = " << obj.m_intCount << endl; cnt = 0 foo(obj); destroy cout << "Count = " << obj.m_intCount << endl; cnt = -1 return 0; destroy } 08.02.2010 The Big Three 2
  • 3.
    Special member functions  Special member functions in C++ are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer. The special member functions are:  Default constructor (if no other constructor is explicitly declared)  Copy constructor  Copy assignment operator  Destructor  The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three:  Copy constructor  Copy assignment operator  Destructor class CDefault { public: CDefault(); // default constructor CDefault(const CDefault& object); // copy constructor virtual ~CDefault(); // default destructor Cdefault& operator = (const CDefault& object); // copy assignment operator }; 08.02.2010 The Big Three 3
  • 4.
    Overloaded member function classCDummy { public: CDummy(int p) : m_intValue(p) { cout << "create" << m_intValue << endl; } virtual ~CDummy() { cout << "destroy" << endl; } CDummy(const CDummy& object) {m_intValue = object.m_intValue;} // polymorphic functions (different data types) void overloaded(void) { cout << "overload " << ++m_intValue << endl; } void overloaded(void) const { cout << "overload const " << m_intValue << endl; } int m_intValue; }; int main(void) { CDummy const dd(2); dd.overloaded(); CDummy bb(3); bb.overloaded(); Cdummy const obj(bb); // copy constructor can be without const Cdummy cobj(obj); // copy constructor must be with const } 08.02.2010 The Big Three 4
  • 5.
    Default class declaration classCDefault { public: // default constructor CDefault(); // copy constructor CDefault(const CDefault& object); // default destructor ~CDefault(); // copy assignment operator CDefault& operator = (const CDefault& object); // Optional // Note: overloaded operator should be overloaded with const also CDefault& operator*(); CDefault const& operator*() const; CDefault* operator->(); CDefault const* operator->() const; }; 08.02.2010 The Big Three 5
  • 6.
    References  Special memberfunctions  Assignment operator in C++  Type polymorphism 08.02.2010 The Big Three 6