/* ** $Id: initialize.cc 609 2007-03-22 20:32:25Z phf $ ** ** Study me, oh dear apprentice, as I will reveal some of the many ** mysteries of C++ object initialization! Oh, and please ignore ** the warnings, it's the C establishment trying to hold you back... */ #include using std::cout; using std::endl; class Probe { public: Probe() { cout << "Default constructor!" << endl; } Probe(int i) { cout << "Custom constructor!" << endl; } Probe(const Probe &that) { cout << "Copy constructor!" << endl; } const Probe& operator=(const Probe &that) { cout << "Assignment operator!" << endl; } }; class Plain { Probe _p; public: Plain(Probe p) { _p = p; } }; class PlainReference { Probe _p; public: PlainReference(Probe &p) { _p = p; } }; class Fancy { Probe _p; public: Fancy(Probe p): _p(p) {} }; class FancyReference { Probe _p; public: FancyReference(Probe &p): _p(p) {} }; int main() { cout << "--- Making probe! ---" << endl; Probe p; cout << "--- Making plain! ---" << endl; Plain a(p); cout << "--- Making fancy! ---" << endl; Fancy b(p); cout << "--- Making plain reference! ---" << endl; PlainReference c(p); cout << "--- Making fancy reference! ---" << endl; FancyReference d(p); cout << "--- You decide! :-) ---" << endl; return 0; }