Yesterday wrote two programs, all appeared the destructor causes the debug assertion failed problem, because is the beginner C + + How to think also can not figure out where the problem. This morning after the guidance of the people finally understand the problem lies. The following code and Problem resolution: (The following excerpt from my CSDN forum for help in the post)
First question procedure:
Teacher_level.h
#pragma once
#include <iostream>
using namespace Std;
int num=0;
Class Teacher
{
Public
Char *title;
Teacher ()
{
Title=new CHAR[50];
}
~teacher ()
{
cout<< "called:" <<++num<<endl;
if (title!=null)
{
Delete[] title;
Title=null;
}
}
};
Class level
{
Public
Char *position;
Level ()
{
Position=new CHAR[50];
}
~level ()
{
if (position!=null)
{
Delete[] position;
Position=null;
}
}
};
Class Teacher_level:public Teacher,public level
{
Public
void Show ()
{
cout<< "The Teacher_level:" <<endl;
cout<< "Title:";
Puts (title);
cout<< "Position:";
puts (position);
}
};
Main.cpp
#include "teacher_level.h"
#include <iostream>
using namespace Std;
int main ()
{
Teacher_level p;
P.title= "Professor";
p.position= "Header";
P.show ();
return 0;
}
The error is as follows:
The second problem procedure:
CShop.h
#pragma once
#include <string.h>
#include <iostream>
using namespace Std;
Class Cshop
{
Public
Char *product;
int price;
Cshop ();
Cshop (char *cproduct,int cprice);
~cshop ();
Friend ostream& operator<< (ostream& os,cshop p)
{
cout<< "Product:";
Puts (p.product);
cout<< "Price:";
cout<<p.price;
return OS;
}
};
Cshop::cshop ()
{
Product=new CHAR[50];
}
Cshop::cshop (char *cproduct,int cprice)
{
Product=new CHAR[50];
strcpy (product,cproduct);
Price=cprice;
}
Cshop::~cshop ()
{
if (product!=null)
{
Delete[] Product;
Product=null;
}
}
Main.cpp
#include "CShop.h"
#include <iostream>
using namespace Std;
int main ()
{
Char *product= "book";
int price=120;
Cshop p (product,price);
cout<< "The information of the shop:" <<endl;
cout<<p<<endl;
return 0;
}
The error is as follows:
Correct answer:
The first one:
Teacher_level p; This invokes the constructor, assigning the title memory
P.title= "Professor"; It also directly points to the constant area, causing the destructor to delete a constant-area pointer
You should assign a value to the title according to the idea of the second procedure.
The second program has a slightly more complex problem.
Friend ostream& operator<< (ostream& os,cshop p)
The p here is passed by value, and the compiler calls the copy constructor to create a new object. But you didn't write the copy constructor yourself, so the compiler generated a copy constructor itself, and the problem arises because the compiler's copy constructor is so-called "shallow copy," which simply copies the address of the product. Then destroy the new object and delete the product's address. You might say that after the delete the product is assigned null, but that is for that temporary object, the product pointer of P inside main does not change, also points to the original new result, and then exits main when the delete again, resulting in an error.
Changing the parameters of this << overloaded function to &p can bypass this problem, but the fundamental solution is to write a copy constructor yourself, instead of simply copying the pointer, and then re-strcpy the new pointer.
I learned a lot about the problems of both programs, especially the second one that made me understand the invocation mechanism of the copy constructor.
The problem of debug assertion failed caused by C + + destructor