943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 439
  • C++ RSS
May 10th, 2009
0

even this simple code?? c'mon c++

Expand Post »
I have this code:
in my judgement it should print 1 and 2, instead it prints 0 and 1
some advice pls... Im using dev c++




C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6. A()
  7. {
  8. arr = new int[2];
  9. }
  10. ~A(){ delete [] arr; }
  11.  
  12. int * arr ;
  13. };
  14.  
  15.  
  16. int main()
  17. {
  18. int * n ;
  19.  
  20. {//induced code block
  21. A aObj;
  22. aObj.arr[0] = 1;
  23. aObj.arr[1] = 2;
  24. n = aObj.arr;
  25.  
  26. /*
  27.  BTW I assume n now points to the same add of aObj.arr ... am I wrong??
  28. */
  29. }
  30.  
  31. std::cout << "\n" << (*n) << " " << (*n+1);
  32. system("pause");
  33. }
Last edited by Ancient Dragon; May 11th, 2009 at 12:20 am. Reason: add line numbers
Similar Threads
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
May 11th, 2009
0

Re: even this simple code?? c'mon c++

The problem is scoping -- object A goes out of scope before the cout statement on line 31. Delete the { and } on lines 20 and 29 and your program will work as you expect it to.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 11th, 2009
0

Re: even this simple code?? c'mon c++

good, thanks I should've seen that
the morale is dont let garbage printed on the screen fool you even if it looks extremly similar too what you expect
Last edited by namehere05; May 11th, 2009 at 12:28 am.
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
May 11th, 2009
0

Re: even this simple code?? c'mon c++

Keep in mind that this line (see red part):

std::cout << "\n" << (*n) << " " << (*n+1);

displays aObj.arr[0] + 1 , not aObj.arr[1] . In your case, they are the same, but if you change line 23 to equal something other than 2, you'll still get 1 2 as output.
Last edited by VernonDozier; May 11th, 2009 at 12:33 am.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

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: Updating prices in a 2d array
Next Thread in C++ Forum Timeline: Inputting data into C ...





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


Follow us on Twitter


© 2011 DaniWeb® LLC