Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: All
May 5, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

The "Big Three Rule" or the "Big Two Rule"?

The famous "Big Three Rule" says that if a class needs any of the Big Three member functions (copy constructor, assignment operator, and destructor), it needs them all. Generally, this rule refers to classes that allocate memory from the free store. However, many other classes require only that the Big Two (copy constructor and assignment operator) be defined by the user; the destructor, nonetheless, is not always required.
class Year
  int y;
  bool cached; //has this object instance been cached? 
  Year(int y);
  Year(const Year& other) //cached should not be copied
    y = other.getYear();
  Year& operator =(const Year&other) //cached should not be copied
    y = other.getYear(); 
    return *this;
  int getYear() const { return y; }
};//note: no destructor is required for class Year
In this example, Class Year does not allocate memory from the free store nor does it acquire any other resources during its construction. A destructor is therefore unnecessary. However, the class needs a user-defined copy constructor and assignment operator to ensure that value of the member 'cached' is not copied because it is calculated for every individual object separately.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date