Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- struct Expression;
- struct Number;
- struct BinaryOperation;
- struct FunctionCall;
- struct Variable;
- struct pstorage
- {
- pstorage(Expression *pt = 0) : ptr(pt)
- {
- if (pt == 0)
- count = 0;
- else
- count = 1;
- }
- pstorage(Expression *pt, int count_) : ptr(pt), count(count_){}
- ~pstorage()
- {
- delete ptr;
- }
- Expression *ptr;
- int count;
- };
- struct SharedPtr
- {
- Expression& operator*() const {
- return *ptr_->ptr;
- }
- Expression* operator->() const {
- return ptr_->ptr;
- }
- // реализуйте следующие методы
- //
- explicit SharedPtr(Expression *ptr = 0) {
- if (ptr != 0)
- ptr_ = new pstorage(ptr);
- else ptr_ = 0;
- }
- ~SharedPtr() {
- if (ptr_ != 0)
- {
- ptr_->count--;
- if (ptr_->count <= 0)
- {
- delete ptr_;
- }
- }
- }
- SharedPtr(const SharedPtr &a) {
- if (a.ptr_ != 0)
- {
- ptr_ = a.ptr_;
- ptr_->count++;
- }
- else
- ptr_ = 0;
- }
- SharedPtr& operator=(const SharedPtr &a) {
- if (this==&a || a.ptr_ == ptr_) return *this;
- if(a.ptr_ == 0) {
- if(ptr_ != 0) {
- ptr_->count--;
- if(ptr_->count == 0)
- delete ptr_;
- };
- ptr_ = a.ptr_;
- return *this;
- }
- else
- {
- if(ptr_ != 0) {
- ptr_->count--;
- if(ptr_->ptr==0)
- delete ptr_;
- ptr_ = a.ptr_;
- ptr_->count++;
- return *this;
- }
- else
- {
- ptr_ = a.ptr_;
- ptr_->count++;
- return *this;
- }
- };
- }
- Expression* get() const {
- if (ptr_ != 0)
- return ptr_->ptr;
- else return 0;
- }
- void reset(Expression *ptr = 0) {
- if(ptr_ != 0 && ptr == ptr_->ptr)
- return;
- if (ptr == 0) {
- if(ptr_ != 0) {
- ptr_->count--;
- if(ptr_->count == 0)
- delete ptr_;
- }
- ptr_ = 0;
- }
- else {
- if(ptr_ != 0) {
- ptr_->count--;
- if(ptr_->count == 0)
- delete ptr_;
- }
- ptr_ = new pstorage(ptr);
- }
- }
- pstorage *ptr_;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement