Home > Back-end >  About why I C code destructor?
About why I C code destructor?

Time:10-21

Simplify the code like this:
 
Class A
{
Public:
Int length ();
}
Class B
{
Private:
A * A;
Public:
A * getA () {return A};
}
Int main ()
{
B B;
EtA (int len=b.g.) length ();
}

Results the destructor is called my b content changed, want to along while didn't want to see,, save the children!!!!!!!!!

CodePudding user response:

It doesn't run wrong? B.g etA () returns null pointer, or do you omitted to b.a initialization code?

CodePudding user response:

GetA () returns a is not initialized
For the uninitialized pointer, calls its methods is bound to go wrong

CodePudding user response:

I have a problem, simplify the source these are initialized, and then I modify and add a function in a class B
 
Void getLength ()
{
Return a - & gt; The length ();
}

Good, but I don't know the specific reason,

CodePudding user response:

 # include 
Class A
{
Public:
Int length=9;
};
Class B
{
Private:
A * A;
Public:
(B)
{
A=new a;
}
A * getA () {return A; }
};
Int main ()
{
B B;
Int len=b.g etA () - & gt; Length;
Len: printf (" % d \ n ", len);
return 0;
}


Len: 9

Process finished with exit code 0

CodePudding user response:

reference 4 floor studyyyyyyyyyy response:
I have a problem, simplify the source these are initialized, then I changed, added a function in a class B
 
Void getLength ()
{
Return a - & gt; The length ();
}

Good, but I don't know the specific reason,


Members of the pointer call them use - & gt;

CodePudding user response:

Write the wrong but my length is a member function
My problem is to use the bidirectional circular linked list implementation of arbitrary length integer addition and subtraction, and then my class is A linked list class, class B is integer, then the getA () is to get the head node, length () is the length of the access to the list, when I wrote the int len=b.g etA () - & gt; The length (); My the b list inside a destructor didn't (empty lists), don't know why, the return value is the return a; Local variables are the destructor?
For the first time in this BBS, always write wrong, sorry!

CodePudding user response:

Also did not see you the destructor

CodePudding user response:

Must be new, simplified is wrong, you write in the constructor
  • Related