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; }