Introduction to C ++ new and delete keywords, static member attributes and functions, usage of this pointer, and delete keywords

Source: Internet
Author: User

Introduction to C ++ new and delete keywords, static member attributes and functions, usage of this pointer, and delete keywords
New and delete keywords

The new and delete keywords are provided in C ++. new is used to open up the memory, delete is used to release the memory, and new automatically calls the constructor. The delete keyword automatically calls the destructor, unlike malloc and free in C, the two functions do not call constructor and destructor.

They have the same functions when you do not need to call constructor and destructor.

// C language syntax int * p = (int *) malloc (sizeof (int); * p = 200; // C ++ syntax int * p2 = new int; * p2 = 200; delete p2; // in the C ++ syntax, a memory is allocated and int * p3 = new int (200) is initialized; delete p3; // generate an array int * pArray = (int *) malloc (sizeof (int) * 2) in C language; p [0] = 1; free (p ); // The C ++ syntax assigns an array int * pArray = new int [2]; pArray [0] = 2; delete [] pArray; // This method does not call the constructor and destructor Test * pT1 = (Test *) malloc (sizeof (Test); free (pT1 ); // new can call constructor, and delete can automatically call the destructor. The function is to open up the memory and release the memory Test * pT2 = new Test (10); delete pT2;

Therefore, we recommend that you use the new and delete syntaxes in C ++, because in the analysis, you may need to describe other objects with the delete syntax, which makes it safer.

Static member attributes and functions

If you want a property to be shared by all such objects, you can define it as a static member property. Similarly, You can reference this property in other classes.

Class T {public: static int a;} // during initialization, it must be initialized outside to prevent repeated memory int T: a = 100; int main () {cout <
 
 
 
  

The above describes how to set the attributes of a class to static. You can also set a function to a static member function.

#include "iostream"using namespace std;class T {public:    int c;    static int a;    static void test() {        cout << "hello" << endl;    }};int T::a = 1;int main() {    T::a = 2;    cout << T::a << endl;    T::test();    system("pause");    return 0;}

T: test (); is used to directly access the function. However, if you use a non-static member attribute or function in the test () method, the compiler reports an error, for example, if you use the c variable in the test function and the c variable is non-static, since it is non-static, it belongs to an object, and if you reference this object in the static function, the compiler cannot identify the object it belongs to. Unless you use an object to introduce this attribute, it is not allowed for non-static member functions to be used in static member functions. The principle is the same as above.

This pointer

In C ++, the compiler manages non-static member attributes, static member attributes, non-static member functions, and static member functions. In fact

Member attributes-stored in objects, same as struct

Static member variables-stored in the global Zone

Member functions-stored in code blocks

Static member functions-stored in the global Zone

By writing the above Code, you may think of a problem. Since the member function is not stored in the current object, we use this object to call this function, when assigning values to the member attributes of the current object, how does the compiler know which object is being assigned? Which Member attribute should I use this function to assign values to? In fact, when the compiler is dealing with this situation, it will add a this pointer, and then point to the object to be assigned a value, and then assign a value to the object. The following uses the C language to simulate the compiler to implement object-oriented encapsulation.

struct Test{    int i;};void Test_init(Test * this,int i){    this->i = i;}int Test_getI(Test * this){    return this->i;}void Test_Print(){    printf("This is class Test. \n");}Test a;Test_init(&a,100);Test_getI(&a);Test_Print();

If you convert the above code into a C ++ implementation

class Test{private:    int i;public:    Test(int i){        this->i = i;    }    int getI(){        return this->i;    }    static void print(){        printf("This is class Test.\n");    }};Test a(100);a.getI();Test::print();

C ++ and C implementations are allocated. In fact, the C ++ compiler helps us save the this pointer parameter.

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.