`
weihe6666
  • 浏览: 430905 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

C++ new and delete

阅读更多
C++ new and delete


摘自:C++_Primer_Plus_4th
Classes Whose Constructors Use new
Classes that use the new operator to allocate memory pointed to by a class member require several precautions in the design. (Yes, we summarized these precautions recently, but the rules are very important to remember, particularly because the compiler does not know them and, thus, won't catch your mistakes.)

1.Any class member pointing to memory allocated by new should have the delete operator applied to it in the class destructor. This frees the allocated memory.

2.If a destructor frees memory by applying delete to a pointer that is a class member, then every constructor for that class should initialize that pointer either by using new or by setting the pointer to the null pointer.

3.Constructors should settle on using either new [] or new, but not a mixture of both. The destructor should use delete [] if the constructors use new [], and it should use delete if the constructors use new.

4.You should define a copy constructor that allocates new memory rather than copying a pointer to existing memory. This enables a program to initialize one class object to another. The constructor normally should have the following form of prototype:

className(const className &)

5.You should define a class member function overloading the assignment operator and having the following form of function definition (here c_pointer is a member of the c_name class and has the type pointer-to-type_name):

c_name & c_name::operator=(const c_name & cn)
{
    if (this == & cn_)
        return *this;     // done if self-assignment
    delete c_pointer;
    c_pointer = new type_name[size];
    // then copy data pointed to by cn.c_pointer to
    // location pointed to by c_pointer
    ...
    return *this;
}

分享到:
评论

相关推荐

    Nucleus C++ v1.3 Upgrade

    support for the newer C++ array operators, new[] and delete[]. “C” callable allocation routines are supplied, including malloc()/free(). These routines are compatible in real-time with the C++ new ...

    Effective C++(第三版)

    use the same form in corresponding uses of new and delete. 条款17:以独立语句将newed对象置入智能指针 store newed objects in smart pointers in standalone statements. 4. 设计与声明 designs and ...

    Bloodshed Dev-C++

    * WebUpdate will now backup downloaded DevPaks in Dev-C++\Packages directory, and Dev-C++ executable in devcpp.exe.BACKUP * Big speed up in function parameters listing while editing * Bug fixes ...

    More Effective C++

    Understand the different meanings of new and delete 异常(Exceptions) 044 条款9:利用 destructors 避免泄漏资源 045 Use destructors to prevent resource leaks 条款10:在 constructors 内阻止资源泄漏...

    由一次程序崩溃引起的对 new 表达式的再次学习 - 文章 - 伯乐在线1

    根据《C++ Primer, Fourth Edition》中5.11节The new and delete Expressions中关于new的描述,new

    新手学习C++入门资料

    C++中new和delete是对内存分配的运算符,取代了C中的malloc和free。 标准C++中的字符串类取代了C标准C函数库头文件中的字符数组处理函数。 C++中用来做控制态输入输出的iostream类库替代了标准C中的stdio函数库。...

    两个CADODatabse class and the CADORecordset class,这是为了在C++中更好地进行ADO数据库操作,同时带有示例程序

    两个CADODatabse class and the CADORecordset class,这是为了在C++中更好地进行ADO数据库操作 主要的类及其函数罗列如下: The CADODatabase Class CADODatabase Open Execute GetRecordsAffected ...

    BSTR详解三

    另一方面,不能用熟悉的C/C++函数进行对BSTR的分配、释放和处理,例如malloc, free, new, delete, lstrcat, and lstrlen 等函数不能用于处理BSTR。就像对接口指针和类指针的处理不一样,对BSTR的处理和对TCHAR*的...

    EffectiveC++ and more Effective C++

     ·条款八:理解各种不同含义的new和delete  ·条款九:使用析构函数防止资源泄漏  ·条款十:在构造函数中防止资源泄漏  ·条款十一:禁止异常信息(exceptions)传递到析构函数外  ·条款十二:理解...

    深度探索模C++对象模型PDF

    6.2 new和delete运算符 针对数组的new语意 Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 ...

    深度探索C++对象模型 超清版

    6.2 new和delete运算符 针对数组的new语意 Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 ...

    From C to C++

    // A C++ Program without class and object! #include using namespace std; const int N=200; void strUpper(char *s); void strLower(char *s); int main(){ char ms[N]; cout; cin.getline(ms,N); ...

    C++第一次大作业

    //print all the boys using the removeSomeElement() method and delete //them int numberOfBoys = boys.Size(); cout ; for(int i = 0; i; i++) { Person & boy = boys.RemoveElement(); boy.Print(); ...

    c++ Effective STL(中文+英文)

    条款7: 当使用new得指针的容器时,切记在容器销毁前delete那些指针 条款8: 千万不要把auto_ptr放入容器中 条款9: 小心选择删除选项 条款10: 当心allocator的协定和约束 条款11: 了解自定义allocator的正统使用法...

    C++实现五子棋代码

    #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif HCURSOR m_hcursor; ///////////////////////////////////////////////////////////////////////////// // CMainFrame ...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    6.2 new和delete运算符 针对数组的new语意 Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 ...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    3.24 Operators new and delete...... . 120 3.24.1 Vector Instead of Array..... . . . 123 3.24.2 String Instead of char*..... . . . 124 3.25 Software Engineering: Program Correctness.... 124 3.26 Dr. P...

    数据结构、算法与应用:C++语言描述(原书第2版)第二部分

    Data Structures, Algorithms, and Applications in C++, Second Edition 出版者的话 译者序 前言 第一部分 预备知识 第1章 C++回顾 1.1 引言 1.2 函数与参数 1.2.1 传值参数 1.2.2 模板函数 1.2.3 引用参数 ...

    Visual C++ 2010入门经典(第5版)--源代码及课后练习答案

    4.3.2 new和delete操作符 168 4.3.3 为数组动态分配内存 169 4.3.4 多维数组的动态分配 171 4.4 使用引用 172 4.4.1 引用的概念 172 4.4.2 声明并初始化lvalue引用 172 4.4.3 声明并初始化rvalue引用 173 ...

    数据结构所有代码C++实现

    cout<<"memory allocate fail and exit"; exit(1); } cin>>temp->num; tail->next=temp; tail=temp; tail->next=NULL; } } void out_put_list() { node* a=head->next; while(a!=NULL) { cout...

Global site tag (gtag.js) - Google Analytics