[ create a new paste ] login | about

Link: http://codepad.org/5s6uQWYv    [ raw code | output | fork ]

johannes - C++, pasted on Sep 27:
class A
{
public:
    A(const char *info)
        : m_info(new std::string(info))
    {
        std::cout << "allocating object resources\n";
    }
 
    ~A()
    {
        delete m_info;
        std::cout << "deallocating object resources\n";
    }
 
    A(const A &other)
        : m_info(new std::string(*other.m_info))
    {
        std::cout << "copying data\n";
    }
 
    A &operator =(const A &other)
    {
        if (this != &other) {
            delete m_info;
            m_info = new std::string(*other.m_info);
            std::cout << "allocating + copying data\n";
        }
        return *this;
    }

    A(A &&other)
    {
        m_info = other.m_info;
        other.m_info = 0;
    }
 
    inline std::string info() const { return *m_info; }
 
private:
    std::string *m_info;
};
 
int main()
{
    std::vector<A> v;
 
    A a("banana");
    v.push_back(std::move(a));
 
    A b("mayonnaise");
    v.push_back(std::move(b));
 
    A c("aardvark");
    v.push_back(std::move(c));
 
    std::cout << "------------\n";
 
    std::cout << v.at(0).info() << "\n";
    std::cout << v.at(1).info() << "\n";
    std::cout << v.at(2).info() << "\n";
 
    std::cout << "------------\n";
 
    return 0;
}


Output:
1
2
Line 32: error: expected ',' or '...' before '&&' token
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: