ex4-13.cpp - Example 4-13 Why write a copy constructor?

// File: ex4-13.cpp - Why write a copy constructor?

#include <iostream>

class ABC
{
    int * p;
public:

    ABC(int P = 0)
    {
        p = new int(P);
    }

    ~ABC()
    {
        delete p;
    }

    int value() const
    {
        return *p;
    }
};

void print(ABC object)
{
    std::cout << object.value() << std::endl;
}

int main()
{
    ABC x(2);
    print(x);
    ABC y(3);
    print(y);
    print(x);
}

/* MS Visual C++ 2008 output with runtime error 

2
3
-572662307

*/

/*  NetBeans 6.5.1 under Windows Vista

2
3
1628741076
     13 [sig] example 4136 _cygtls::handle_exceptions: Error while dumping state
 (probably corrupted stack)
/cygdrive/c/Users/Joe/.netbeans/6.5/bin/dorun.sh: line 103:  4136 Segmentation f
ault      (core dumped) "$pgm" "$@"

*/

/* gnu 3.3.2 on Linux

2
3
0

*/



CIS27: Programming in C++    Instructor: Joe Bentley