Some problems such as C + + constructors and destructors

Source: Internet
Author: User

Although the C language has been learned, but some of the basic C + + is not very understanding, but also need to master. Vaughn also began to talk about C + + design mode, must quickly see, or will be white nest drops money.

For memory leaks, my personal understanding is that the program in the process of running, I opened up space, after the use of this space has not been released. I made this kind of low-level error tonight, causing the program not to run, or to look at the code first:

#include <iostream>/* Run this program using the console Pauser or add your own getch, System ("pause") or input loop */using namespace Std;class Person{public:person () {cout<< "base class constructor execution ... \ n";}       ~person () {cout<< "base class destructor in execution ... \ n";}; Class Ds:public Person{public:ds () {cout<< "derived class constructor in execution ... \ n";} ~ds () {cout<< "derived class destructor in execution ... \ n";}; int main (int argc, char** argv) {DS P;return 0;}

This code is not a problem, the program began to run from the main function, instantiate a derived class DS an object p, regardless of how the derived class DS always call the base class person's constructor, and then derived class DS calls its own constructor, followed by its own destructor, The last is the constructor of the base class person, and the result is as follows:

In fact, when this is the base class construction (destructor), when to tune the derived class constructs (destructors), my personal understanding can be expressed with a simple graph, the base class construction and destruction is like a large framework containing the construction and destruction of derived classes:

1. For the above procedure I continue to modify in the main function , if new, but not delete, (with matching and compatibility, the result is the same)

int main (int argc, char** argv) {ds *p=new DS ();
        Person *p= new DS ();
return 0;}

At this point, the problem arises, if in C + +, the following result will appear:


The reason is new, but no delete, causing a memory leak, in the process of running the destructor is not called, until the end of the program, the system will automatically free memory.

2. For the above procedure I continue to modify in the main function, this time with delete p, now match the case of the operation, that is, the pointer of the derived class points to the derived object:

int main (int argc, char** argv) {ds *p=new DS (); Delete  P;return 0;}


The results of the operation should also be guessed out, 4 all have

3. For the above procedure I continue to modify in the main function, this time using compatibility, that is, the base class pointer to the object of the derived class:

<strong>int Main (int argc, char** argv) {person *p=new DS (); Delete  P;return 0;} </strong>

But this time again, there is no destructor for the derived class in the running result, which is a curse of compatibility, because the base-class pointer can only point to the part of the derived class that inherits its own, and for the part of the derived class DS, the base-class pointer is not mapped, so it is not called, and the result is as follows:

To solve this problem is really not difficult, this time, the base class pointer on the base class pointer, tube he 3,721, I am this time only in the base class person's destructor to add a virtual (virtual attribute), although in the main function, or the third case. I can also easily output the following results:

In fact, this involves, inheritance (compatible rules), polymorphic knowledge, in the C + + involved in the pattern, 95% are used to polymorphism, undoubtedly this is the focus of C + +, we must learn this piece.

Knowledge of inheritance and polymorphism the next blog is to say, that will be capitalized close-up,, haha, that is a very interesting example-daughter-in-law cooking, Factory mode, slowly into the core of C + + to go.

Next, in the C + +, the pointer delete delete is worth the problem, this piece is necessary to say:

Just send the code up.

#include <iostream>/* Run this program using the console Pauser or add your own getch, System ("pause") or input loop */using namespace Std;int Main (int argc, char** argv) {int *p;p=new int;*p=3;     cout<< "Output value *p=" <<*p<<endl;     cout<< "Output address p=" <<p<<endl;          Delete p;   Delete p after       cout<< "Delete p output value *p=" <<*p<<endl;     cout<< "Delete p after output address p=" <<p<<endl;                 return 0;}


I define an integer pointer p,new an int type integer unit and assign a value of 3 to *p, after that, the address of the output *p and P, after which I am in delete p. In fact, every time we delete actually deletes the value that P points to that space and does not delete its address, the following result is shown:

Well, my limited ability can only be written here, after encountering problems in the perfect, the wrong place also to be modified, sleeping,,, sleepy dead,,,,

Some problems such as C + + constructors and destructors

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.