943,609 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 556
  • C++ RSS
Feb 9th, 2009
0

referencing an object

Expand Post »
I just have one question, when you have a class that is defined:
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 9th, 2009
0

Re: referencing an object

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.

C++ Syntax (Toggle Plain Text)
  1. class box{
  2. public:
  3. int value;
  4. box()
  5. {
  6. value=0;
  7. };
  8. box(box &c)
  9. {value=c.value;};
  10. };
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Feb 10th, 2009
0

Re: referencing an object

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 12th, 2009
1

Re: referencing an object

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 13th, 2009
0

Re: referencing an object

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 13th, 2009
0

Re: referencing an object

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Overload operator with one operand.
Next Thread in C++ Forum Timeline: Beginners Trouble





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC