ot vesso(7-08-2005)
reiting (30)
[ dobre ]
[ zle ]
Variant za otpechatvane
Primeren kod na
cpp |
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <memory>
#include
<boost/shared_ptr.hpp>
class my_class {
private:
std::string m_name;
public:
my_class(const
std::string& name): m_name(name)
{std::cout << "creating
'" << m_name << "'\n";}
my_class(const
my_class& other): m_name("copy of
" + other.m_name)
{std::cout << "creating
'" << m_name << "'\n";}
~my_class() {std::cout << "deleting '" << m_name <<
"'\n";}
void use(const std::string & usage)
const
{std::cout << "Using
'" << m_name << "':
" << usage << "\n";}
};
void
use_by_reference(my_class & object)
{
object.use("from function
use_by_reference()");
}
void
use_by_pointer(my_class * object)
{
object->use("from function
use_by_pointer()");
}
bool is_blue_moon() {return true;}
void may_throw_exception()
{
std::cout << "Throwing an
exception...\n";
throw
std::string("exception");
}
my_class static_class("static
sample");
void auto_example()
{
std::vector<my_class> vector_of_objects;
std::vector<my_class *> vector_of_pointers;
my_class object("auto
object");
object.use("as object");
use_by_reference(object);
use_by_pointer(&object);
vector_of_objects.push_back(object);
vector_of_pointers.push_back(&object);
for (int index = 2; index < 4;
++index) {
std::ostringstream oss;
oss << "auto
" << index;
my_class inner(oss.str());
vector_of_objects.push_back(inner);
vector_of_pointers.push_back(&inner);
}
vector_of_objects[0].use("as
the first object in the vector");
vector_of_objects[2].use("as
the last object in the vector");
vector_of_pointers[0]->use("as the first pointer in the vector");
}
void dynamic_example()
{
my_class * pointer(new my_class("dynamically allocated"));
std::vector<my_class *> vector_of_pointers;
vector_of_pointers.push_back(pointer);
pointer->use("as
pointer");
use_by_reference(*pointer);
use_by_pointer(pointer);
vector_of_pointers[0]->use("as the first pointer in the vector");
delete pointer;
}
void simple_leak()
{
my_class * pointer(new my_class("*simple leak"));
}
void another_simple_leak()
{
my_class * pointer(new my_class("*another simple leak"));
pointer = new
my_class("another object...");
delete pointer;
}
void conditional_leak()
{
my_class * pointer(new my_class("*conditional leak"));
if (is_blue_moon())
return;
delete pointer;
}
void leak_on_exception()
{
my_class * pointer(new my_class("*leak on exception"));
may_throw_exception();
delete pointer;
}
void simple_autoptr()
{
std::auto_ptr<my_class> pointer(new my_class("simple auto_ptr"));
pointer->use("as
pointer");
use_by_pointer(pointer.get());
use_by_reference(*pointer);
}
void conditional_autoptr()
{
std::auto_ptr<my_class> pointer(new my_class("conditional auto_ptr"));
if (is_blue_moon())
return;
pointer.reset();
}
void
autoptr_and_exception()
{
std::auto_ptr<my_class> pointer(new my_class("auto_ptr and exception"));
may_throw_exception();
}
void shared_ptr_demo()
{
typedef
boost::shared_ptr<my_class> sp_my_class;
std::vector<sp_my_class> vector;
sp_my_class example1(new my_class("the first shared_ptr object"));
example1->use("as
pointer");
use_by_pointer(example1.get());
use_by_reference(*example1);
vector.push_back(example1);
example1.reset();
for (int index = 2; index < 4;
++index) {
std::ostringstream oss;
oss << "shared_ptr " << index;
sp_my_class inner(new my_class(oss.str()));
vector.push_back(inner);
}
vector[0]->use("as
vector[0]");
vector[1]->use("as
vector[1]");
sp_my_class example2(new my_class("another shared_ptr object"));
example2 = vector[2];
may_throw_exception();
}
int main()
{
std::cout << "program
started.\n";
auto_example();
dynamic_example();
simple_leak();
another_simple_leak();
conditional_leak();
try {
leak_on_exception();
} catch (const std::string exception) {
std::cout << "caught exception\n";
}
simple_autoptr();
conditional_autoptr();
try {
autoptr_and_exception();
} catch (const std::string exception) {
std::cout << "caught second exception\n";
}
try {
shared_ptr_demo();
} catch (const std::string exception) {
std::cout << "caught third exception\n";
}
std::cout << "program
ended.\n";
return 0;
}
|
P.S. Po niakakva prichina v rezhim "pregled"
statiiata izglezhdashe dobre, a sled publikuvaneto niakoi redove
se preformatiraha.
Originalniiat tekst e go ima na:
http://home.exetel.com.au/v/test1.cpp
Kirilitsata e na utf8.
Molia ueb-mastura da opravi protsesa na publikuvane - ako
vuobshte e vuzmozhno.
P.P.S Statiiata izglezhda dobre vuv "Variant za
otpechatvane"
<< Programirane pod Linuks. Vuvedenie za nachinaeshti. | T2 Project ima nuzhda ot pomosht >>
|