• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
The Big Three
 

The Big Three

on

  • 2,269 views

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 ...

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

Statistics

Views

Total Views
2,269
Views on SlideShare
2,202
Embed Views
67

Actions

Likes
0
Downloads
11
Comments
0

3 Embeds 67

http://okolovich.info 61
http://www.linkedin.com 5
http://www.slideshare.net 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    The Big Three The Big Three Presentation Transcript

    • 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