[ create a new paste ] login | about

Link: http://codepad.org/72tZmoWx    [ raw code | output | fork ]

C++, pasted on May 15:
    #include <ostream>
    #include <iomanip>

    struct Base
    {
        int aMemberForASize;
        Base() { std::cout << "Constructing a base." << std::endl; }
        Base(const Base&) { std::cout << "Copying a base." << std::endl; }
        ~Base() { std::cout << "Destroying a base." << std::endl; }
    };

    struct Derived : public Base
    {
        int aMemberThatMakesMeBiggerThanBase;
        Derived() { std::cout << "Constructing a derived." << std::endl; }
        Derived(const Derived&) : Base() { std::cout << "Copying a derived." << std::endl; }
        ~Derived() { std::cout << "Destroying a derived." << std::endl; }
    };

    int SomeThirdPartyMethod(Base /* SomeBase */)
    {
        return 42;
    }

    int main()
    {
        Derived derivedObject;
        {
            //Scope to show the copy behavior of copying a derived.
            Derived aCopy(derivedObject);
        }
        SomeThirdPartyMethod(derivedObject);
    }


Output:
1
2
3
4
5
6
7
8
9
10
Constructing a base.
Constructing a derived.
Constructing a base.
Copying a derived.
Destroying a derived.
Destroying a base.
Copying a base.
Destroying a base.
Destroying a derived.
Destroying a base.


Create a new paste based on this one


Comments: