Home > Back-end >  Why the second output not & C code 1111111, I returned from the add in front of the object
Why the second output not & C code 1111111, I returned from the add in front of the object

Time:10-30

#include
using namespace std;
The class Array
{
Private:
Int * p;
int n;
Public:
Array () {}
Array (int * a, int b) {p=a; N=b; }
Array (const Array& Arr) {p=Arr. P; N=Arr. N. }
Array operator + + ()
{
for (int i=0; I & lt; n; I++)
{
* (p + I) +=1;
}
Return * this;
}
Array operator + + (int)
{
Array temp=* this;
for (int i=0; I & lt; n; I++)
{
* (p + I) +=1;
}
return temp;
}
Friend Array operator - (Array& A)
{
for (int i=0; I & lt; A.n. I++)
{
* (Amy polumbo + I)=1;
}
Return A;
}
Friend Array operator - (Array& A, int)
{
Array temp=A;
for (int i=0; I & lt; A.n. I++)
{
* (Amy polumbo + I)=1;
}
return temp;
}
Void the print ()
{
for (int i=0; I & lt; n; I++)
{
cout <* (p + I) & lt; <" ";
}
cout }
~ Array () {}
};
Int main ()
{
Int H [7], I;
For (I=0; I & lt; 7. I++)
{
cin> H [I];
}
* Array Arr=new Array (H, I);
cout <"Output lead since added:";
(+ + Arr) (*). Print ();
cout <"Rear output first lead since the add again since the add results:";
(* (Arr) + +). Print ();
cout <"Rear output first lead since the add again since the deceleration preceded the result:";
(Arr) (*). Print ();
cout <"Rear output first lead since the add again since plus rear again lead the decrement again since minus results:";
(Arr) (* -). Print ();
The delete Arr.

}

CodePudding user response:

Question is very simple, you copy function, copy the new object of p and original object * this p points to address is the same, so the back * this array p content changed, a new object also can see the same information, so you print is 2222222 (* this p points to an array into a 2222222, a new object p point to the same array)
Array operator + + (int)
{
Array temp=* this;//t p and p * this point to the same address

for (int i=0; I & lt; n; I++)
{
* (p + I) +=1;
}
return temp;
}

So the biggest problem is the copy constructor and function, should be to let the new object p points to a new memory, then the p points to an array of information copy of the original object, rather than directly to a new object of p is equal to the original p
So modify the constructor

Array (int * a, int b) {p=new int [b]; Memcpy (p, a, sizeof (int) * b); N=b; }//p points to new array
Array (const Array& Arr) {p=new int [Arr. N]; Memcpy (p, Arr. J p, sizeof (int) * Arr. N); N=Arr. N. }//p point to the new array, rather than is the same as the original array

Because the constructor to p allocated memory, so the destructor to free memory
~ Array () {if (p) delete [] p; }



CodePudding user response:

reference 1st floor qybao response:
problem is very simple ah, your copy function, copy the new object of p and original object * this p points to address is the same, so the back * this array p content changed, a new object also can see the same information, so you print is 2222222 (* this p points to an array into a 2222222, a new object p point to the same array)
Array operator + + (int)
{
Array temp=* this;//t p and p * this point to the same address

for (int i=0; I & lt; n; I++)
{
* (p + I) +=1;
}
return temp;
}

So the biggest problem is the copy constructor and function, should be to let the new object p points to a new memory, then the p points to an array of information copy of the original object, rather than directly to a new object of p is equal to the original p
So modify the constructor

Array (int * a, int b) {p=new int [b]; Memcpy (p, a, sizeof (int) * b); N=b; }//p points to new array
Array (const Array& Arr) {p=new int [Arr. N]; Memcpy (p, Arr. J p, sizeof (int) * Arr. N); N=Arr. N. }//p point to the new array, rather than is the same as the original array

Because the constructor to p allocated memory, so the destructor to free memory
~ Array () {if (p) delete [] p; }

WC, bosses

CodePudding user response:

reference 1st floor qybao response:
problem is very simple ah, your copy function, copy the new object of p and original object * this p points to address is the same, so the back * this array p content changed, a new object also can see the same information, so you print is 2222222 (* this p points to an array into a 2222222, a new object p point to the same array)
Array operator + + (int)
{
Array temp=* this;//t p and p * this point to the same address

for (int i=0; I & lt; n; I++)
{
* (p + I) +=1;
}
return temp;
}

So the biggest problem is the copy constructor and function, should be to let the new object p points to a new memory, then the p points to an array of information copy of the original object, rather than directly to a new object of p is equal to the original p
So modify the constructor

Array (int * a, int b) {p=new int [b]; Memcpy (p, a, sizeof (int) * b); N=b; }//p points to new array
Array (const Array& Arr) {p=new int [Arr. N]; Memcpy (p, Arr. J p, sizeof (int) * Arr. N); N=Arr. N. }//p point to the new array, rather than is the same as the original array

Because the constructor to p allocated memory, so the destructor to free memory
~ Array () {if (p) delete [] p; }

Thank you bosses inspiration.
  • Related