C + + Basics Getting Started Tutorial (vi): Why do I need new when I create a class? _c language

Source: Internet
Author: User

Today is the final introduction to pointers, and it allows beginners to understand why classes are new, why certain variables are released, and why Wood is so clever.

1. Create a dynamic structure using new

You remember our structure, right? As I said before, a struct can also create a storage space by using new, and returns a pointer to the type space of the struct body.

The following code:

Copy Code code as follows:

struct MANS
{
int age;
int IQ;
};
man* Pman = new Man;
Pman->iq = 251;
cout << pman->iq << "\ n";
cout << (*pman). IQ << "\ n";

Defines a structure man, and then uses the new man to dynamically create memory space, returning a pointer to a memory space that holds the type of man.
Then call Pman->iq to assign a value to the property, here the "->" symbol we should all very attribute it.

To illustrate, if you're not creating an object with new, you should use the. symbol to use the attribute.
If you create an object with new, use the-> symbol to use the attribute.

But in fact, eventually. The symbolic,-> symbol is just a grammatical candy.
Because Pman is a pointer, the *pman is the object value on the memory space that the pointer refers to, so the invocation property is this: (*pman). IQ
But every time so call, very troublesome, so there is a pman->iq this convenient form.

So, let's not get mixed up again. and->.
Generally, it can be understood that pointers use-> symbols.

(This text feel very bad explanation, because someone next to me is always talking, my thoughts are not very quiet ...) )

2. Why should there be new?

Why should there be new? Why should I create objects dynamically? Why sometimes you don't need new, and sometimes you use new, like:

Copy Code code as follows:

Cocos2d-x3.x's value class, we're all familiar with it.
Value v = value (100);
Cocos2d-x's Sprite class, it's also very property.
sprite* sp = new Sprite ();


Why is it that some places don't need new, and some places have to be new?

This involves automatic storage and dynamic storage.

3. Automatic storage (automatic variables, local variables)

Automatic storage, also known as an automatic variable, such as int num = 10; This num is part of an automatic variable.
The so-called automatic, on behalf of it will automatically apply for memory, will automatically release the memory, automatic variables are stored in the stack (LIFO first out).

If you find it difficult to understand, then change to a name-local variable.
That's a good idea, isn't it? A local variable is released after it leaves the function or leaves the code block it belongs to.

and value V = value (100); , int num = 10; These are local variables that are freed once they leave the function or leave its scope.
such as the int num; As a member variable, the NUM variable is also released when the class is freed.
That's why we don't have to release them after we've created so many basic types of variables like int, float, and so on.
Because they are automatically released.

4. Dynamic storage

The big advantage of automatic variables is that we don't need to manage memory, but sometimes we don't want to have this kind of automatic free memory.

We want to control when we release the object, and then we need to use new.

We all know that after new, if the corresponding delete is not invoked, the memory space requested will never be released.
This is the dynamic storage, we ourselves to apply for memory, ourselves to free memory.

Of course, one of the culprits of memory leaks is new~!

Because normal people can be negligent, and when the program is large enough, the logic is complex enough, some places call new, but inadvertently delete is not normal.

Of course, the role of new may not only be in this, the book is not in-depth introduction, I do not say, lest the wrong, after all, C + + still need to be more rigorous ~

I do not dare to blow water ~

5.vector and Array

It is believed that many beginners will be confused by the vector of cocos2d-x3.x.

Vector is a class in the Cocos2d-x package, and vector is a class provided in C + +.

A capital letter, a lowercase letter, don't be mistaken la ~

The vector of C + + is an implementation of a dynamic array, and we all know that the array is declared to determine the size of the array, unless it is used in the new way.

The vector is using new to request memory, but it is already encapsulated and does not require us to deal with the problem of memory release.

Vectors are used in a very simple way:

Copy Code code as follows:

Can be initialized at the time of the Declaration
Vector<int> v = {1, 2};
You can add new elements dynamically.
V.push_back (5);
Use at to get an element
cout << v.at (2);
You can also get an element like an array
cout << v[2];

Note that using vector to introduce header files: #include <vector>
Since it is a dynamic array, it is certain that elements can be dynamically added and deleted.
There are two ways to get an element, one is to use at, which is safe, and checks to see if the subscript is legitimate.
You can also use an ordinary array to get the element, which is dangerous and does not check if the subscript is legitimate.

Well, Vector doesn't say much.

There is also an array class, which is added by C++11.
Vector is a dynamic array, and the efficiency is naturally inferior to some.
Ordinary arrays may be less convenient and secure to use.
As a result, array is born, array is also fixed-length arrays, but it may be more convenient and safe to use:

Copy Code code as follows:

Array<int, 2> arr = {1, 2};
cout << arr.at (1);

The way you declare it is special, you need to set the type and size of the array, because it is a fixed-length array, and the size is immutable.
There are still two ways to get an element, using an at or an ordinary array, and the difference between the two methods is the same as the vector.

6. End

Well, the contents of the fourth chapter of the book are over here.
This chapter of the memory is quite messy, what kind of knowledge is introduced some of it, but not too deep.
Recently, there are many things, hope to continue to insist.

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.