CIS-255 Home http://www.c-jump.com/bcc/c255c/c255syllabus.htm

C++ object lifecycle


  1. Controlling object creation and destruction
  2. Controlling object creation
  3. Constructors
  4. Sub-object Constructors
  5. Default constructors
  6. Other constructors
  7. Rules for implicit default constructors
  8. Function default parameters
  9. More function default parameters
  10. Dynamic memory allocation
  11. Destructors
  12. Chain of Destructors
  13. Destructor Calls
  14. Using destructors
  15. References
  16. Reference examples
  17. References themselves can't be changed
  18. More on references
  19. Rreference Trapdoor
  20. Copy constructors
  21. A need for a copy constructor
  22. Copy constructor example
  23. Calling copy constructors

Controlling object creation and destruction


Controlling object creation


Constructors


Sub-object Constructors


Default constructors



    // point.h
    class Point
    {
        public:
            Point();
        private:
            int m_x;
            int m_y;
    };

    // point.cpp
    // Default constructor is the one with no arguments:
    Point::Point()
    {
        m_x = 0;
        m_y = 0;
    }

Other constructors



    // point.h
    class Point
    {
        public:
            Point(); // default constructor
            Point( int x, int y );
        private:
            int m_x;
            int m_y;
    };

    // point.cpp
    Point::Point( int x, int y )
    {
        m_x = x;
        m_y = y;
    }

Rules for implicit default constructors


  • you add a constructor taking no arguments:
class Point {
public:
    Point();
};
  • you add a constructor with all default arguments:
class Point {
public:
    Point( int x = 0, int y = 0 );
};
  • you add a constructor taking one or more arguments:
class Point {
public:
    Point( Point const* other );
};
void main()
{
    Point pt; // Error: no default constructor:
}

Function default parameters


More function default parameters


Dynamic memory allocation


Destructors


Chain of Destructors


Destructor Calls


  • ~Point() is the destructor for
    class Point.
  • You don't call destructors yourself !
  • Destructors do not:
    • take args (so they cannot be overloaded);
    • they do not return anything.
// point.h
class Point
{
    public:
        ~Point(); // destructor
    private:
        int m_x;
        int m_y;
};
// point.cpp
#include <iostream>
#include "point.h"
Point::~Point()
{
    std::cout << "Goodbye...";
}
// main.cpp
#include "point.h"
void main()
{
    Point pt;
}// ~Point() destructor is invoked here

Using destructors


References


Reference examples



    void swap( int& left_, int& right_ )
    {
        int temp = left_;
        left_ = right_;
        right_ = temp;
    }

    void f()
    {
        int x = 8;
        int i = 4;
        int& ri = i;
        ri++;          // now i == 5
        ri = x;        // now i == 8
        int& ref2 = 2; // error, RHS must be an lvalue
    }

References themselves can't be changed


More on references


Rreference Trapdoor


  • Returning a reference to a member is a trapdoor:

  • Trapdoor is a secret way of gaining access to a program, online service, or other resource. Trapdoors are built into the software by the original programmers as a way of gaining special access to particular software functions.


// point.h
class Point
{
public:
    int& refx();
private:
    int m_x;
    int m_y;
};

// point.cpp
#include "point.h"
int& Point::refx()
{
    return m_x;
}

// main.cpp
#include "point.h"
void main()
{
    Point pt;
    pt.refx() = 14;
}

Copy constructors


A need for a copy constructor

    //point.h
    class Point {
    public:
        int  m_coord[ 2 ];
        int* m_ptr;

        Point() {
            m_coord[ 0 ] = -1;
            m_coord[ 1 ] = -1;
            m_ptr = m_coord;
        }
        
        void reset() {
            m_ptr[ 0 ] = 0;
            m_ptr[ 1 ] = 0;
            m_ptr = 0;
        }
    };//class Point
//main.cpp
#include <cassert>

void main()
{
    Point pt1;
    Point pt2;
    draw_segment( pt1, pt2 );

    assert( pt1.m_coord[ 0 ] == -1 );
    assert( pt1.m_ptr != 0 );
}
    //point.cpp
    void draw_segment( Point from, Point to )
    {
        // Draw a line:
        // ...
        // Then reset objects:
        from.reset(); // Hmm?..
        to.reset();
    }

Copy constructor example


Calling copy constructors