标签:cat delete struct 成员 rev 目的 ons share code
// make_pair example
#include <utility> // std::pair
#include <iostream> // std::cout
int main () {
std::pair <int,int> foo;
std::pair <int,int> bar;
foo = std::make_pair (10,20);
bar = std::make_pair (10.5,‘A‘); // ok: implicit conversion from pair<double,char>
std::cout << "foo: " << foo.first << ", " << foo.second << ‘\n‘;
std::cout << "bar: " << bar.first << ", " << bar.second << ‘\n‘;
return 0;
}
// pair::operator= example
#include <utility> // std::pair, std::make_pair
#include <string> // std::string
#include <iostream> // std::cout
int main () {
std::pair <std::string,int> planet, homeplanet;
planet = std::make_pair("Earth",6371);
homeplanet = planet;
std::cout << "Home planet: " << homeplanet.first << ‘\n‘;
std::cout << "Planet size: " << homeplanet.second << ‘\n‘;
return 0;
}
// make_shared example
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo = std::make_shared<int> (10);
// same as:
std::shared_ptr<int> foo2 (new int(10));
auto bar = std::make_shared<int> (20);
auto baz = std::make_shared<std::pair<int,int>> (30,40);
std::cout << "*foo: " << *foo << ‘\n‘;
std::cout << "*bar: " << *bar << ‘\n‘;
std::cout << "*baz: " << baz->first << ‘ ‘ << baz->second << ‘\n‘;
return 0;
}
// allocate_shared example
#include <iostream>
#include <memory>
int main () {
std::allocator<int> alloc; // the default allocator for int
std::default_delete<int> del; // the default deleter for int
std::shared_ptr<int> foo = std::allocate_shared<int> (alloc,10);
auto bar = std::allocate_shared<int> (alloc,20);
auto baz = std::allocate_shared<std::pair<int,int>> (alloc,30,40);
std::cout << "*foo: " << *foo << ‘\n‘;
std::cout << "*bar: " << *bar << ‘\n‘;
std::cout << "*baz: " << baz->first << ‘ ‘ << baz->second << ‘\n‘;
return 0;
}
// static_pointer_cast example
#include <iostream>
#include <memory>
struct A {
static const char* static_type;
const char* dynamic_type;
A() { dynamic_type = static_type; }
};
struct B: A {
static const char* static_type;
B() { dynamic_type = static_type; }
};
const char* A::static_type = "class A";
const char* B::static_type = "class B";
int main () {
std::shared_ptr<A> foo;
std::shared_ptr<B> bar;
foo = std::make_shared<A>();
// cast of potentially incomplete object, but ok as a static cast:
bar = std::static_pointer_cast<B>(foo);
std::cout << "foo‘s static type: " << foo->static_type << ‘\n‘;
std::cout << "foo‘s dynamic type: " << foo->dynamic_type << ‘\n‘;
std::cout << "bar‘s static type: " << bar->static_type << ‘\n‘;
std::cout << "bar‘s dynamic type: " << bar->dynamic_type << ‘\n‘;
return 0;
}
foo‘s static type: class A
foo‘s dynamic type: class A
bar‘s static type: class B
bar‘s dynamic type: class A
// static_pointer_cast example
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo;
std::shared_ptr<const int> bar;
foo = std::make_shared<int>(10);
bar = std::const_pointer_cast<const int>(foo);
std::cout << "*bar: " << *bar << ‘\n‘;
*foo = 20;
std::cout << "*bar: " << *bar << ‘\n‘;
return 0;
}
// get_deleter example
#include <iostream>
#include <memory>
struct D { // a verbose array deleter:
void operator()(int* p) {
std::cout << "[deleter called]\n";
delete[] p;
}
};
int main () {
std::shared_ptr<int> foo (new int[10],D());
int * bar = new int[20];
// use foo‘s deleter to delete bar (which is unmanaged):
(*std::get_deleter<D>(foo))(bar);
return 0;
// foo‘s deleter called automatically
}
struct D {
void operator()(int* p) {
std::cout << "";
delete[] p;
}
}
(*std::get_deleter<D>(foo)) // 还是一个删除器
(*std::get_deleter<D>(foo))(bar); // 删除bar
// shared_ptr swap specialization
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo (new int(10));
std::shared_ptr<int> bar (new int(20));
swap(foo,bar);
std::cout << "foo: " << *foo << ‘\n‘;
std::cout << "bar: " << *bar << ‘\n‘;
return 0;
}
// shared_ptr i/o
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo (new int (10));
std::cout << " foo: " << foo << ‘\n‘;
std::cout << "*foo: " << *foo << ‘\n‘;
return 0;
}
输出结果是
foo: 0x920d90
*foo: 10
a review at operator overloading(3)
标签:cat delete struct 成员 rev 目的 ons share code
原文地址:https://www.cnblogs.com/sjl473/p/14243379.html