Home > Back-end >  Newbie on the road, have what problem, the following code at present thought is b need a no argument
Newbie on the road, have what problem, the following code at present thought is b need a no argument

Time:10-08

Class B {
Public:
B (const B& B)
{m_ptr=b.m _ptr; }
Void the Create ()
{m_ptr=new unsigned char [100]. }
Void Destroy ()
{the delete m_ptr; M_ptr=NULL; }
Private:
Unsigned char * m_ptr;
};
Int _tmain (int arg c, _TCHAR * argv [])
{
B B;
Biggest reate ();
B, b1 (B);
B.D estroy ();
B1. XXXX ();
return 0;
}

CodePudding user response:

Calls the default constructor, it is necessary to give m_ptr pointer declaration space

CodePudding user response:

You need in the no-arg constructor, give m_ptr new a memory

CodePudding user response:

When you were in b1 structure m_ptr point to area is by the application of b, b is released b1 is still there, so will go wrong, the solution has two: one, a new application for new memory when constructing b1, released separately and release; Two, the introduction of a pointer counter, when the counter is greater than 0 don't release the memory block, is equal to zero release again
  • Related