a question of struct copys
-
#include <iostream.h>
struct structA
{
int iMember;
char *cMember;
};int main(int argc, char* argv[])
{
structA instant1,instant2;char c = 'a';
instant1.iMember = 1;
instant1.cMember = &c;
instant2 = instant1;cout << *(instant1.cMember) << endl;
*(instant2.cMember) = 'b';
cout << *(instant1.cMember) << endl; //I want to know why change instant2 and the instant1 change!getchar();
} -
#include <iostream.h>
struct structA
{
int iMember;
char *cMember;
};int main(int argc, char* argv[])
{
structA instant1,instant2;char c = 'a';
instant1.iMember = 1;
instant1.cMember = &c;
instant2 = instant1;cout << *(instant1.cMember) << endl;
*(instant2.cMember) = 'b';
cout << *(instant1.cMember) << endl; //I want to know why change instant2 and the instant1 change!getchar();
}wbgxx wrote:
char c = 'a';
-here you make c equal to 'a'
wbgxx wrote:
instant1.cMember = &c;
-here you make the cMember pointer of instant1 point at the variable c, so instant1.cMember now contains the memory location of c
wbgxx wrote:
instant2 = instant1;
-here you copy the contents of instant1 into instant2, so the members in instant2 (iMember, cMember) will contain the same values as the corresponding ones in instant1. This means, instant2.cMember now also points at your c variable, both instant1.cMember and instant2.cMember contains the memory address of the variable c.
wbgxx wrote:
cout << *(instant1.cMember) << endl;
-here you say "print out the value that is stored in the memory location pointed at by instant1.cMember", this memory location happens to be c's, and since you set c to 'a', it will print 'a'.
wbgxx wrote:
*(instant2.cMember) = 'b';
- here you say, "copy the value 'b' to the memory location pointed at by instant2.cMember", which is -as you remember- the same as instant1.cMember, the address of your c variable.
wbgxx wrote:
cout << *(instant1.cMember) << endl;
- here you say now, "print out the value that is stored in the memory location pointed at by instant1.cMember" again, but since this memory location is the same as the one stored in instant2, and previously you changed the value stored in that memory location to 'b', it will print 'b'. This is basic pointer stuff, i recommend reading some good books or checking out online tutorials about pointers in C/C++...
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
#include <iostream.h>
struct structA
{
int iMember;
char *cMember;
};int main(int argc, char* argv[])
{
structA instant1,instant2;char c = 'a';
instant1.iMember = 1;
instant1.cMember = &c;
instant2 = instant1;cout << *(instant1.cMember) << endl;
*(instant2.cMember) = 'b';
cout << *(instant1.cMember) << endl; //I want to know why change instant2 and the instant1 change!getchar();
}When you do
instant2 = instant1;
it does a bitwise copy. This meansinstant1
andinstant2
will be exactly alike. Which means the address pointed to byinstant1.cMember
andinstant2.cMember
will be the same. And so changing the value using any of the pointers will reflect in both the pointers. This is a classic example of why we need a copy constructor and assignment operator.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
#include <iostream.h>
struct structA
{
int iMember;
char *cMember;
};int main(int argc, char* argv[])
{
structA instant1,instant2;char c = 'a';
instant1.iMember = 1;
instant1.cMember = &c;
instant2 = instant1;cout << *(instant1.cMember) << endl;
*(instant2.cMember) = 'b';
cout << *(instant1.cMember) << endl; //I want to know why change instant2 and the instant1 change!getchar();
}wbgxx wrote:
cout << *(instant1.cMember) << endl; //I want to know why change instant2 and the instant1 change!
I want to know why you think instant2 has changed; all you did is look at instant1 :doh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).