复制构造函数
复制构造函数,传入同类型其他对象,初始化成员 。
1
| Message(const Message &m) :contents(m.contents), folders(m.folders){} ;
|
操作符重载
1 2
| Message& operator=(const Message&);
|
析构函数
变量在超出作用域时自动撤销,动态分配对象被删除时调用。
消息处理示例
Message.h
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 37 38 39 40 41 42 43 44
| #pragma once #include<string> #include<set> #include<vector> #include<iostream> #include "Folder.h" using namespace std;
class Message { friend class Folder; public: Message(const string &str = "") :contents(str) {}; Message(const Message &m) :contents(m.contents), folders(m.folders) { put_msg_in_folders(folders); }; Message& operator=(const Message&); ~Message();
void save(Folder&); void remove(Folder&);
vector<Folder*> get_folders(); string print_message() { return contents; } void debug_print();
private: string contents; set<Folder*> folders;
void put_msg_in_folders(const set<Folder*>&); void remove_msg_from_Folders();
void addFolder(Folder *f) { folders.insert(f); }; void remFolder(Folder *f) { folders.erase(f); }; };
|
Message.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include "Message.h" #include "Folder.h"
Message::~Message() { remove_msg_from_Folders(); }
Message & Message::operator=(const Message &rhs) { if (&rhs != this) { remove_msg_from_Folders(); contents = rhs.contents; folders = rhs.folders; put_msg_in_folders(rhs.folders); } return *this; }
void Message::save(Folder &f) { folders.insert(&f); f.addMsg(this); }
void Message::remove(Folder &f) { folders.erase(&f); f.remMsg(this); }
vector<Folder*> Message::get_folders() { return vector<Folder*>(folders.begin(), folders.end()); }
void Message::debug_print() { cerr << "Message:\n\t" << contents << endl; cerr << "Appears in " << folders.size() << " Folders " << endl; }
void Message::put_msg_in_folders(const set<Folder*> &rhs) { for (set<Folder*>::const_iterator beg = rhs.begin(); beg != rhs.end(); ++beg) (*beg)->addMsg(this); }
void Message::remove_msg_from_Folders() { for (set<Folder*>::const_iterator beg = folders.begin(); beg != folders.end(); ++beg) (*beg)->remMsg(this); }
|
Folder.h
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
| #pragma once #include <set> #include "Message.h"
class Folder { friend class Message; public: ~Folder(); Folder(const Folder&); Folder& operator=(const Folder&);
Folder() {}; void save(Message& msg); void remove(Message& msg);
vector<Message*> messages(); void debug_print();
private: typedef set<Message*>::const_iterator Msg_iter; set<Message*> msgs; void copy_msgs(const set<Message*>&); void empty_msgs(); void addMsg(Message *m) { msgs.insert(m); }; void remMsg(Message *m) { msgs.erase(m); }; };
|
Folder.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include "Folder.h" #include "Message.h"
Folder::~Folder() { empty_msgs(); }
Folder::Folder(const Folder &f) { copy_msgs(f.msgs); }
Folder & Folder::operator=(const Folder &f) { if (&f != this) { empty_msgs(); copy_msgs(f.msgs); } return *this; }
void Folder::save(Message & msg) { msgs.insert(&msg); msg.addFolder(this); }
void Folder::remove(Message & msg) { msgs.erase(&msg); msg.remFolder(this); }
vector<Message*> Folder::messages() { return vector<Message*>(msgs.begin(), msgs.end()); }
void Folder::debug_print() { cerr << "Folder contains " << msgs.size() << "messages " << endl; int ctr = 1; for (Msg_iter beg = msgs.begin(); beg != msgs.end(); ++beg) cerr << "Message " << ctr++ << ":\n\t" << (*beg)->print_message << endl; }
void Folder::copy_msgs(const set<Message*> &m) { for (Msg_iter beg = m.begin(); beg != m.end(); ++beg) (*beg)->save(*this); }
void Folder::empty_msgs() { Msg_iter it = msgs.begin(); while (it != msgs.end()) { Msg_iter next = it; ++next; (*it)->remove(*this); it = next; } }
|
智能指针
通过记录指针被复制次数,避免产生悬垂指针。
普通含有指针成员的类 HasPtr.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #pragma once class HasPtr { public: HasPtr(int *p, int i) :ptr(p), val(i) {}
int *get_ptr()const { return ptr; } int get_val() const { return val; }
void set_ptr(int *p){ptr = p; } void set_val(int v) { val = v; }
int get_ptr_val() const { return *ptr; } void set_ptr_val(int val)const { *ptr = val; } private: int *ptr; int val; };
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <iostream> #include <sstream> #include "HasPtr.h"
using namespace std; int main() { int obj = 0; HasPtr ptr1(&obj, 42); HasPtr ptr2(ptr1);
ptr1.set_ptr_val(22); cout << ptr2.get_ptr_val() << endl; return 0; }
|
引入指针计数器 U_Ptr.h
1 2 3 4 5 6 7 8 9 10 11
| #pragma once #include "HasPtr.h"
class U_Ptr { friend class HasPtr; int *ip; size_t use; U_Ptr(int *p) :ip(p), use(1) {}; ~U_Ptr() { delete ip; }; };
|
HasPtr.h
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
| #pragma once #include "U_Ptr.h"
class HasPtr { public: HasPtr(int *p, int i) :ptr(new U_Ptr(p)), val(i) {} HasPtr(const HasPtr &orig) :ptr(orig.ptr), val(orig.val) {}
HasPtr& operator=(const HasPtr&);
~HasPtr() { if (--ptr->use == 0) delete ptr; } private: U_Ptr *ptr; int val; };
HasPtr& HasPtr::operator=(const HasPtr &rhs) { ++rhs.ptr->use; if (--ptr->use == 0) delete ptr; ptr = rhs.ptr; val = rhs.val; return *this; }
|
我好懒