Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class: Special Topics Copy Constructors Static members Friends this

Similar presentations


Presentation on theme: "Class: Special Topics Copy Constructors Static members Friends this"— Presentation transcript:

1 Class: Special Topics Copy Constructors Static members Friends this
For classes using memory allocation Destructors Deep vs. Shallow Copy Assignment Operator

2 Copy constructor A copy constructor is a special constructor that takes a class instance of the same type as an argument It is called automatically when the compiler needs to make a copy of a class instance Ex: when passing a class instance to a function “by value” Input argument should always be a const reference MyClass(const MyClass&); const ensures that the argument will not be modified Reference ensures that the argument will not be copied Otherwise, the copy constructor would be called again to make the copy Infinite loop!!!!

3 Static members A static member of a class is specified using the keyword static static const string seasons[]; // in header file ClassName::seasons[]= { “spring”, “summer”, “winter”, “fall” }; // in .cpp file Member functions can also be static! Static members do not belong to any instance of the class They are “global” Static members are accessed using the class name directly string now= ClassName::seasons[2]; An instance is not required (no “.” operator!) – and should not be used

4 Friends of a class A friend of a class is an object or function that is Not a member of the class, but Is allowed access to the private members of the class anyway A friend is declared in the class definition using the keyword friend friend ostream &operator<<( ostream &, const ClassName& ); // in class definition Function is granted access to private data members Usage of friends, while allowed, is discouraged It violates the basic OOP principle of encapsulation

5 Destructors A destructor is a method that is called automatically when a class instance is destroyed (de-allocated from memory) The opposite of a constructor which is used to create a class instance Defined with a tilde (and no parameters): ~ClassName(); Explicit definition is not required If not defined, compiler will call a default destructor and automatically remove all members Destructor should be defined if the class does explicit memory allocation (ex: using the new operator) Destructor should explicitly free any allocated memory (ex: using delete) If not done, pointer to memory will be destroyed, but memory will remain allocated with no way to reference it!

6 Deep vs. shallow copy Important to do a deep copy when making a copy of a class instance where memory is explicitly allocated in a class (ex: using new) Avoids problem of having compiler attempts to access deallocated memory Shallow copy Just copy the original pointer value to the pointer in the copied instance Result is that both the original and copied pointers have the same value i.e. point to the same memory location Deep copy Allocate new memory in the copied instance Copy the contents of the original instance to the new memory Result is two independent instances with their own copy of the same data values

7 Assignment operator Must test for self-assignment Otherwise…
If the class does memory allocation Ex: A = A Otherwise… When the existing storage is deallocated, there will be nothing to copy!!! Note also necessary in copy constructors!!! MyClass& MyClass::operator=(const MyClass &rhs) { // Check for self-assignment! if (this == &rhs) // Same object? return *this; // Yes, so skip assignment, // and just return *this. ... // Deallocate, allocate new space, // copy values... return *this; // for cascading! }


Download ppt "Class: Special Topics Copy Constructors Static members Friends this"

Similar presentations


Ads by Google