M
Matteo Settenvini
Ok, I'm quite a newbie, so this question may appear silly. I'm using
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to "browse" it). So I
expected that when I run this program, I get both c1.A and c2.A
pointing to the same address, and changing c1.A means that also c2.A
changes too.
----- BEGIN example CODE -----------
#include <iostream>
using std::cout;
using std::endl;
class C {
private :
int A[3];
public:
C() { for( int i=0; i<3; i++ ) A = 0; };
void alter() { A[1]++; };
void printA() const;
};
void
C:rintA() const
{
for( int i=0; i<3; i++ )
cout << "A[" << i
<< "] is " << A
<< endl;
}
int
main( )
{
C c1, c2;
c1.alter();
c2 = c1; // Why does this also make a copy of the array??
cout << " c2 is : " << endl;
c2.printA();
c1.alter();
cout << " c2 is still : " << endl;
c2.printA();
return 0;
}
----- END example CODE -----------
Instead, a copy of the array is made, so touching c1.A doesn't affect
c2.A.
Is this a _c++ standard_ behaviour? My lucky guess is that when you
use the default copy constructor, the object at the left of the
assignment gets the memory copied bit-to-bit from the one on the right
side, like a sort of "memcpy()" happens between the two.
So an array declared as an automatic variable in c1 gets its content
copied in the new object (c2) rather than simply the address it's
pointing to, since all the space taken by the array is sequentially
allocated in memory along with other vars declared in the class scope.
Hope my english is understandable enough...
Matteo
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to "browse" it). So I
expected that when I run this program, I get both c1.A and c2.A
pointing to the same address, and changing c1.A means that also c2.A
changes too.
----- BEGIN example CODE -----------
#include <iostream>
using std::cout;
using std::endl;
class C {
private :
int A[3];
public:
C() { for( int i=0; i<3; i++ ) A = 0; };
void alter() { A[1]++; };
void printA() const;
};
void
C:rintA() const
{
for( int i=0; i<3; i++ )
cout << "A[" << i
<< "] is " << A
<< endl;
}
int
main( )
{
C c1, c2;
c1.alter();
c2 = c1; // Why does this also make a copy of the array??
cout << " c2 is : " << endl;
c2.printA();
c1.alter();
cout << " c2 is still : " << endl;
c2.printA();
return 0;
}
----- END example CODE -----------
Instead, a copy of the array is made, so touching c1.A doesn't affect
c2.A.
Is this a _c++ standard_ behaviour? My lucky guess is that when you
use the default copy constructor, the object at the left of the
assignment gets the memory copied bit-to-bit from the one on the right
side, like a sort of "memcpy()" happens between the two.
So an array declared as an automatic variable in c1 gets its content
copied in the new object (c2) rather than simply the address it's
pointing to, since all the space taken by the array is sequentially
allocated in memory along with other vars declared in the class scope.
Hope my english is understandable enough...
Matteo