
Listing 1

#include <iostream.h>

class X
    {
    friend ostream &operator<<(ostream &os, const X &x);
public:
    X(int i) : a(i), b(0) { }
    X(X &x) : a(x.a), b(x.b) { ++x.b; }
    X(const X &x) : a(x.a), b(x.b+1) { }
private:
    int a, b;
    };

ostream &operator<<(ostream &os, const X &x)
    {
    return os << '(' << x.a << ", " << x.b << ')';
    }

int main()
    {
    X x1(3);        // uses X(int)

    cout << "x1 = " << x1 << '\n';

    const X x2(x1); // uses X(X &)

    cout << "x1 = " << x1 << '\n';
    cout << "x2 = " << x2 << '\n';

    X x3(x2);       // uses X(const X &)

    cout << "x2 = " << x2 << '\n';
    cout << "x3 = " << x3 << '\n';
    return 0;
    }

----------

