[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

auto pointer example




Nach der Vorlesung haben wir das auto pointer Beispiel doch noch zum
Laufen gebracht. Das Problem war die Zuweisung des neuen Strings.
Steckt man das ganze in den Konstruktor von auto_ptr<string> geht es
einfach.

/js

#include <memory>
#include <string>
#include <iostream>

using namespace std;

static auto_ptr<string>
foo()
{
    auto_ptr<string> p(new string("hello, world"));
    return p;
}

int
main()
{
    auto_ptr<string> s;

    for (int i = 0; i < 1000000; i++) {
	s = foo();
	cout << *s << endl;
    }

    return 0;
}