Tuesday, July 14, 2009

C++ >> Object and pointer?

I am a newbie in C++ programming.





What is the different effect in using object and using pointer??

C++ %26gt;%26gt; Object and pointer?
In addition to what the previous answerer said, if you use a pointer, you change the values of the object itself. If you use an object then you might change the object itself or just a copy of the object depending on the situation. Here are three examples:





Let's say we have an object called "someObject" and it contains an attribute called number.





Example # 1:





int main( )


{


someObject a;


a.number = 10;


cout %26lt;%26lt; a.number; // This is going to print out 10


function(a); // see what "function" does below


cout %26lt;%26lt; a.number; // This is going to print out 10 not 20


return 0;


}





void function(someObject a)


{


a.number = 20;


}





In the previous example, we are using objects (not pointers to objects). So, if you change the object's values within the scope of where you created the object, then the values of the object itself are going to change. If you pass the object to a function (by value), any changes you make to that object are actually done on a copy of the object not the object that you passed to the function itself. Therefore, assigning 20 to the object in the previous example did not affect the value of (a) within main.





Example #2:


If you still want to use objects, not pointers, and you want the changes you make to the objects to affect the origional object not a copy of the object, you have to pass the object to functions by referrence as follows:





void function(someObject%26amp; a) // note the additional '%26amp;' here


{


a.number = 20;


}





Example #3:


There are two reasons why you would want to use pointers to objects rather than the object itself:





1) Because you want changes to the object anywhere in your program to affect the object itself not a copy of it.





2) Because you want to save space as the previous answerer stated. That's because functions don't create copies of the object you pass to them anymore.





NOTE: passing by referrecne as in the 2nd example does the exact same thing (and has the same benefits) as pointers.





Here is how you would do that:





int main( )


{


someObject * a;


a-%26gt;number = 10;


cout %26lt;%26lt; a-%26gt;number; // This prints out 10


function(a);


cout %26lt;%26lt; a-%26gt;number; // This print out 20


return 0;


}





void function(someObject * a)


{


a-%26gt;number = 20;


}





I hope this helps!!
Reply:When an object is created, space is allocated to all of the data members for that object so it takes up (or can take) a lot of memory space. On the other hand, a pointer is just a reference to the memory address of an object and we know that a memory address will take up a very little space (only 2 bytes in case of integer pointer)
Reply:Can't add much to what Silver Sword wrote, but maybe just a few notes on syntax and symantics on pointers and stuff because it can be confusing for a newb (I was there once).





int a; // a regular int


int* ptrI; // read int* as "pointer to an integer". It literally contains a memory address, it might be something like 0x0bf080c4





ptrI = %26amp;a; // this means "take the address in memory of a and store it in ptrI





*ptrI = 15; // this means "go to the address out in memory ptrI specifies and store the value 15 there"





cout %26lt;%26lt; *ptrI %26lt;%26lt; endl; // this outputs 15


cout %26lt;%26lt; a %26lt;%26lt; endl; // so does this because ptrI points to a





a = 30; // store 30 into a, and since ptrI points to a, *ptrI == 30 as well





The other piece of pointer syntax is -%26gt;. If I have a pointer to a Person object that has a data member called "name", I can access "name" in one of two ways:





Person* p;


(*p).name = "me"; // way 1


p-%26gt;name = "you"; // way 2





Both ways do the same thing.





One other thing about "%26amp;". You can use this in the way I wrote above, or in a function definition:





void foo(int%26amp; x);





int a = 6;


foo(a); // this is implicitly passing the *address* of a to foo. Any change I make to "x" in foo() will affect "a" when foo() is done running.





This is equivalent to this:





void foo(int* x);





int a = 6;


foo(%26amp;a); // this explicitly passes the address of "a" to foo


No comments:

Post a Comment