Pain points from C # To C ++

Source: Internet
Author: User

 

I learned C language directly to C. Now I have to turn to C ++. I personally think that C ++ is the key to learning about its memory allocation.

Next I will use a simple class to study the memory allocation of C ++.

// Definition of the test class
Class Test
{
Public:
Test () {cout <"default" <Endl ;};
Test (int );
Int;
~ Test ();
};
Test: Test (int)
{
Cout <"with Parameters" <Endl;
A = 0;
}
Test ::~ Test ()
{
Cout <"destructor" <Endl;
}

 

// Main function content

Test T1;
Test T2 (3 );
// T1.a = 3;

Cout <t1.a <endl;
Cout <t2.a <endl;

Const int num = 5;
Test t [num];
Cout <t [0]. a <endl;

// Cout <t [0]. a <endl;
For (int I = 0; I <5; I ++)
{
T [I] = test ();
T [I]. a = I;
Cout <(t [I]). a <endl;
}

 

 

Notes:

Test t1; call the default constructor to allocate memory. a is junk content, and t [0]. a is also a garbage value. As for the reason why it is a garbage value, it is because it has not been assigned a value, so if it is initialized for the member in the constructor, it will not be a garbage value. The default constructor provided by the system does not do this.

Test t2 (); this is incorrect, so writing is affected by C #.

Test (); this is acceptable.

For example, cout <endl;
Test ();
T [I] = test ();

Running result: endl default endl destructor (endl; indicates line feed)

From the results, the test () statement can also be executed, and, like the following, a copy constructor is called to generate a temporary class object.

There is no doubt about the dynamic memory allocation (that is, the memory allocation with the new mark), which is very direct.

 

As I am a beginner, please point out the mistakes I have made. Haha

 

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.