referencing an object

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

referencing an object

 
0
  #1
Feb 9th, 2009
I just have one question, when you have a class that is defined:
  1. class box{
  2. public:
  3. int *value;
  4. box()
  5. {
  6. value=new int;
  7. *value=0;
  8. };
  9. box(box &c)
  10. {*value=*c.value;};
  11. };
If in the main you said class box b(a); it would work as expected. But I don't understand how that makes sense. How can you set the adress of the object to another object. It would make sense to set it to another object's adress. Or is it that the reference operator is being used differenly than as an adress getter?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,653
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: referencing an object

 
0
  #2
Feb 9th, 2009
line 10 is wrong. Why is value a pointer? From the class constructure it appears to be just a single integer, and you don't need a pointer for that

And get rid of those stars on line 10.

  1. class box{
  2. public:
  3. int value;
  4. box()
  5. {
  6. value=0;
  7. };
  8. box(box &c)
  9. {value=c.value;};
  10. };
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: referencing an object

 
0
  #3
Feb 10th, 2009
Sorry I was working on complex copies when I screwed line 10 up, but what my real focus was was:

If in the main you said class box b(a); it would work as expected. But I don't understand how that makes sense. How can you set the adress of the object to another object. It would make sense to set it to another object's adress. Or is it that the reference operator is being used differenly than as an adress getter?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: referencing an object

 
1
  #4
Feb 12th, 2009
You're right, it is being used differently. In this case, it is passing the object by reference, which is just C++'s shortcut around pointers. It just means anything you do to the parameter within the method will be permanent.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: referencing an object

 
0
  #5
Feb 13th, 2009
so this is a totally different use of the & operator that I was not aware of? Or is this use of the & operator used in that case only, with objects in functions?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: referencing an object

 
0
  #6
Feb 13th, 2009
The & operator is used to pass variables by reference when in the parameter list of a function. Outside of that context it is always used to get the address of a variable. Othere than this, I am not what you are asking.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 388 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC