学习BoolanC++笔记_02(C++面向对象高级编程(上)第二周)

 

作者: ayaoko

出处: http://www.cnblogs.com/fyc006/>

关于作者:小可才疏学浅还请多多赐教!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(270469391@qq.com)咨询.

7,三大函数:拷贝构造,拷贝复制,析构
 三个特殊函数:
  带指针Mystring class必须需要重写
  拷贝构造(重写:接收到本身,叫拷贝构造)
    无重写  浅拷贝(只拷贝指针,内存泄漏和别名问题)
    有重写  深拷贝(能拷贝指针能容)
  拷贝复制(重写:赋值到本身,叫拷贝赋值)
     检测自我赋值:无检查会引起效率低,出错问题!
  析构函数


8,堆、栈与内存管理
所谓stack栈
           Stack,是存在於某作用域(scope)的一塊内存空間
      (memoryspace)。例如當你調用函數,函數本身即
      會形成一個stack用來放置它所接收的參數,以及返回地址。
            在函數本體(functionbody)内聲明的任何變量,
      其所使用的内存塊都取自上述stack

所谓heap堆
        Heap,或謂systemheap,是指由操作系統提供的
        一塊global内存空間,程序可動態分配(dynamic allocated)從某中獲得若干區塊(blocks)。

 stack objects的生命期
        所謂stackobject其生命在作用域(scope)結束之際結束
        這種作用域内的object,又稱為autoobject,因為它會被「自動」清理。

static stack objects的生命期
        所謂static object其生命在作用域(scope)
        結束之後仍然存在,直到整個程序結束。

global objects的生命期
       所謂globalobject其生命在整個程序結束之後
      才結束。你也可以把它視為一種staticobject其作用域是「整個程序」。

head objects的生命期
       heap object其生命在它被delete之際結束。
      (避免内存泄漏:new delete配套)。

new:1,分配memory;2,转型;3,在调用ctor.
      (operator new 其内部调用malloc)
      (static_cast 转型)  

delete:1,先调用dtor;2,在释放memory.
     (operator delete 其内部调用free)

VC动态分配内存:
     内存 cookie+调试信息(debug/release)+x+补充16倍数。
    cookie 1,记录整块大小;2,借用最后操作系统 给&还 os给1 os还0。

vc动态分配array
    同上,增加个数位。

***array new 一定要搭配 array delete
   内存泄漏

9. 复习String类的实现过程

  string.cpp,string.h.

 1 #include <iostream>
 2 #include "string.h"
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     String s1("hello");
 9     String s2("world");
10 
11     String s3(s2);
12     cout << s3 << endl;
13 
14     s3 = s1;
15     cout << s3 << endl;
16     cout << s2 << endl;
17     cout << s1 << endl;
18     cout << "Hello world!" << endl;
19     return 0;
20 }
View Code
 1 #ifndef STRING_H_INCLUDED
 2 #define STRING_H_INCLUDED
 3 
 4 
 5 class String
 6 {
 7 public:
 8    String(const char* cstr=0);
 9    String(const String& str);
10    String& operator=(const String& str);
11    ~String();
12    char* get_c_str() const { return m_data; }
13 private:
14    char* m_data;
15 };
16 
17 #include <cstring>
18 
19 inline
20 String::String(const char* cstr)
21 {
22    if (cstr) {
23       m_data = new char[strlen(cstr)+1];
24       strcpy(m_data, cstr);
25    }
26    else {
27       m_data = new char[1];
28       *m_data = '\0';
29    }
30 }
31 
32 inline
33 String::~String()
34 {
35    delete[] m_data;
36 }
37 
38 inline
39 String& String::operator=(const String& str)
40 {
41    if (this == &str)
42       return *this;
43 
44    delete[] m_data;
45    m_data = new char[ strlen(str.m_data) + 1 ];
46    strcpy(m_data, str.m_data);
47    return *this;
48 }
49 
50 inline
51 String::String(const String& str)
52 {
53    m_data = new char[ strlen(str.m_data) + 1 ];
54    strcpy(m_data, str.m_data);
55 }
56 
57 #include <iostream>
58 using namespace std;
59 
60 ostream& operator<<(ostream& os, const String& str)
61 {
62    os << str.get_c_str();
63    return os;
64 }
65 
66 
67 #endif // STRING_H_INCLUDED
View Code

 

10. 扩展补充:类模板,函数模板,及其他
 10.1 static:
  static data members:
   1,需要在类外围定义。
  static memeber functions:
   1,无this,可处理static data
   2,调用static函数的方式有二:
    1,通过object调用。
    2,通过class name调用。
 把ctors放到private
  Singleton


 10.2 cout
  多类型重载。
 10.3 扩展
  class template,类模板。
  function template,函数模板。
  namespace:
   1,using directive
    using namespace std;
   2,using declaration
    using std::cout
   3,
 10.4 更多细节

作者:ayaoko
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

posted on 2017-01-22 21:30  ayaoko  阅读(166)  评论(0编辑  收藏  举报

导航