-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathintrusive_ptr.cpp
36 lines (33 loc) · 942 Bytes
/
intrusive_ptr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <boost/intrusive_ptr.hpp>
#include "intrusive_ptr_base.hpp"
class Rectangle : public intrusive_ptr_base<Rectangle> {
public:
Rectangle(int w, int h) : width(w), height(h) {}
~Rectangle() { std::cout << "Rectangle destructor" << std::endl; }
int getArea() { return width * height; }
private:
int width;
int height;
};
int main(){
Rectangle* rectp = new Rectangle(5, 3);
boost::intrusive_ptr<Rectangle> rectsp1(rectp);
std::cout << "refcount: " << rectsp1->refcount() << std::endl;
boost::intrusive_ptr<Rectangle> rectsp2(rectp);
std::cout << "refcount: " << rectsp1->refcount() << std::endl;
//rectsp1.reset(rectp);
std::cout << rectsp1->getArea() << std::endl;
return 0;
}
/*
Default constructor
intrusive_ptr_add_ref...
refcount: 1
intrusive_ptr_add_ref...
refcount: 2
15
intrusive_ptr_release...
intrusive_ptr_release...
Rectangle destructor
*/