C + + Object-oriented advanced Programming (IX) reference with overloaded operator new AND operator delete

Source: Internet
Author: User

Abstract: Technology lies in communication, communication, reproduced please specify the source and maintain the integrity of the work.

A Reference

Reference: As mentioned before, his main function is to alias, similar to pointers, implementation is also based on pointers.

1. The reference must have an initial value and cannot refer to nullptr

2. No reference to others after reference

3. References are not typically used for declaring variables, more for parameter types, and for return value types

See the code below

intMainintargcConst Char*argv[]) {    intx=0; //p is a pointer to x    int* p = &x; //R is a reference to x    int& r =x; //This operation is not r referenced by others but is r referenced by x = 5    intx2 =5; R= X2;//x = 5cout <<"x ="<<sizeof(x) <<Endl; cout<<"r ="<<sizeof(r) <<Endl; cout<<"address of R ="<< &r <<Endl; cout<<"address of P ="<< P <<Endl; return 0;}

Output results

You will find that the address of R is the same as P address, in fact, there is no actual memory reference, in order to implement the false name of the reference alias, the compiler will do so sizeof (R) = sizeof (x) and &x = &r

4. Reference is a pointer, is a nice pointer

void Func2 (string* obj) {obj,clear ();}//pass by pointervoid func1 (string    obj) {obj. Clear ();}  Pass by valuevoid func3 (string& obj) {obj. Clear ();}  Pass by reference

Call End

    string A;     // pass by pointer interface is different    Func2 (&a);         // pass by value call interface is the same low efficiency     func1 (a);         // pass by reference call interface is the same    Func3 (a);

5. Function signatures cannot be distinguished by reference

Double imag (const double& IM) {return  im;}; Double imag (const double  im) {return im;};

Call the time you'll find

The error hint is that the function call is confusing and you will find that the function signature cannot be distinguished by reference (the red part above is the function signature)

Const also counts as part of a function signature

Two polymorphic

With Vaiturl memory there will be a 4-byte pointer

Inheritance is an inherited call rather than an inherited memory size

Three Prerequisites for dynamic binding

1. Must be called through the pointer

2. The pointer is an upward relationship (secure)

3. A virtual function is called

(* (P->VPTR) [n]) (p));

(*p->vptr[n]) (p)

Three const

A.const object (data member must not be changed)

B.non-const object (data member can be changed)

C.const member function (guaranteed not to change data member)

D.non-const member functions (does not guarantee that data member unchanged)

Their combination of relationships

When the const and NON-CONST versions of a member function are present, the Const object will only call the const version, and Non-const object will only call Non-const

Let's think about it.

Const String Str ("HelloWorld"); Str.print ();

If print () does not add Const[const object and Non-const member function] will error

Copy on Write, with Const do not consider COW

class Template std::basic_string<...>//operatorConst {Do not consider cow= =operator[] (Size_type POS) {must consider cow};

Four New and Delete

Mentioned before.

New = malloc () + ctor

Delete = Dtor + free ()

New and delete can not be overloaded, but operator new and operator delete can be overloaded

classfoo{ Public: Foo () {cout<<"Foo ("<< ++ctornum <<")"<<Endl; }    ~Foo () {cout<<"~foo ("<< ++dtornum <<")"<<Endl; }    //overloaded member OPERATPR new    void*operator New(size_t size) noexcept {cout<<"Foo operator New"<<Endl; return malloc(size); }    //Overloaded member OPERATPR Delete    void operator Delete(void*ptr, size_t size) {cout<<"Foo operator Delete"<<Endl;  Free(PTR); }
}

Call End

int Main () {    new  Foo;     Delete p;}

Results

Found out we took over operator new and operator delete

classfoo{ Public: Foo () {cout<<"Foo ("<< ++ctornum <<")"<<Endl; }    ~Foo () {cout<<"~foo ("<< ++dtornum <<")"<<Endl; }    //overloaded member OPERATPR new    void*operator New[] (size_t size) {cout<<"Foo operator New"<<Endl; return malloc(size); }        //overloaded member Operatpr delete[]    void operator Delete[](void*ptr, size_t size) {cout<<"Foo operator delete[]"<<Endl; cout<< size <<Endl;  Free(PTR); }        Static intCtornum; Static intDtornum;}; intFoo::ctornum =0; intFoo::d tornum =0;

Call End

int Main () {    Foonew foo[5];     Delete [] l;     return 0 ;}

Results

We can reload multiple versions of class Menber operator new () the first parameter must be size_t

We can overload class member operator delete () to overload a version, but they will never be deleted

calling the overloaded delete () will only call these overloaded versions of operator delete () if the ctor thrown by new is called exception, which is only possible to be called. Mainly used to return the memory occupied by an object that has not been fully successful in urban construction

The adjustment will correspond to each other, if your delete () and new () does not have one by one corresponding to the compiler will not give an error to discard the exception to do processing abort

     classbad{};//Throw exceptions with    classTest { Public: Test () {} Test (inti) {/*intentionally throwing an exception*/ ThrowBad ();} ~Test () {}//general operator new ()        void*operator New(size_t size) {cout<<"general operator new ()"<<endl;return malloc(size);} //General OPERATPR Delete        void operator Delete(void* ptr, size_t size) {cout<<"General operator Delete ()"<<endl; Free(PTR);} //operator New () of the standard library        void*operator New(size_t size,void* start) {cout<<"operator New () of the standard library"<<endl;returnstart;} //operator Delete () of the standard library        void operator Delete(void*,void*) {cout<<"Standard library operator delete (void*, void*)"<<Endl;} //The new operator new ()        void*operator New(size_t size,LongExtra) {cout<<"new operator new ()"<<endl;return malloc(Size +extra);} //The new operator new ()        void operator Delete(void*,Long) {cout<<"Brand new operator delete (void*, long)"<<Endl;} //another operator new ()        void*operator New(size_t size,LongExtraCharInit) {cout<<"another operator new ()"<<endl;return malloc(Size +extra);} //another operator delete ()        void operator Delete(void* Size,LongExtraCharInit) {cout<<"another operator delete ()"<<Endl;} };

Call and output

intMain () {Try{Test T; Test* P1 =Newtest; Test* P2 =New(&t) test; Test* P3 =New( -) test; Test* P4 =New( -,'a') test; Test* P5 =New( -) Test (1); Test* P6 =New( -,'a') Test (1); Test* P7 =New(&t) Test (1); }    Catch(...)    {}; return 0;}

found that the new and delete are one by one correspondence, as to why the other is not printed out because, new does not successfully throw an exception caused

Finish!

Reference << Houtie C + + Object-oriented advanced programming >>

C + + Object-oriented advanced Programming (IX) reference with overloaded operator new AND operator delete

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.