473,503 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About references in C++.

78 New Member
Hi all. I'm currently learning C++ from a tutorial C to C++. I have a question on references. More precisely, i'm confused why this code gives the following result.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class person
  6. {
  7. public:
  8.  
  9.    char *name;
  10.    int age;
  11.  
  12.    person (char *n = "no name", int a = 0)
  13.    {
  14.       name = new char[100];
  15.       strcpy (name, n);
  16.       age = a;
  17.    }
  18.  
  19.    person (person &s)               // The COPY CONSTRUCTOR
  20.    {
  21.       name = new char[100];
  22.       strcpy (name, s.name);
  23.       age = s.age;
  24.    }
  25.  
  26.    ~person ()
  27.    {
  28.       delete [] name;
  29.    }
  30. };
  31.  
  32. void foo(person p)
  33. {
  34.    cout << p.name << ", age " << p.age << endl << endl;
  35. }
  36.  
  37. person &bar()
  38. {
  39.     return person("Todd", 15);
  40. }
  41.  
  42. int main ()
  43. {
  44.    person k ("John", 56);
  45.    cout << k.name << ", age " << k.age << endl << endl;
  46.  
  47.    foo(k); 
  48.  
  49.    person m(k);
  50.  
  51.    person n = bar();
  52.     foo (n);
  53.  
  54.    return 0;
  55. }
  56.  
result:
------------------------------------------------------
C:\Documents and Settings\Sebouh>C:\my_cpp_tests\cpp_test\debug\cpp _test.exe
John, age 56

John, age 56

John, age 4212483 <----------------- WHY??
-----------------------------------------------------

why not Todd?


Thanks
Apr 15 '07 #1
4 1016
JosAH
11,448 Recognized Expert MVP
Your function bar attempts to return a reference to a local variable of type person.
After functions have returned all local variables have gone and destructed so your
reference refers to garbage.

kind regards,

Jos
Apr 15 '07 #2
Sebouh
78 New Member
So is it "by chance" that i get John?
Apr 15 '07 #3
JosAH
11,448 Recognized Expert MVP
So is it "by chance" that i get John?
Yep, the memory where that object resided on the stack is gone, it's no more,
it's pushing up the daisies, it shuffled off its mortal coil, it's gone to meet its
maker ;-) The content of that memory could be anything.

kind regards,

Jos
Apr 15 '07 #4
Sebouh
78 New Member
Yep, the memory where that object resided on the stack is gone, it's no more,
it's pushing up the daisies, it shuffled off its mortal coil, it's gone to meet its
maker ;-) The content of that memory could be anything.

kind regards,

Jos
Heh, alright then.
Thanks.
Apr 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1570
by: Snake | last post by:
Hi guys, I have question about classes. when u create class called Test. and you define variable Test c; so does this act like( a variable c of type Test pointing to an abject )? The thing that...
21
2078
by: JustSomeGuy | last post by:
When you iterate through a list of objects in a list. list<object> mylist; list<object>::const_iterator iter; object ob; for (iter=mylist.begin(); iter != mylist.end(); ++iter) { ob =...
6
2472
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
24
2080
by: venkatesh | last post by:
hai, any can tell what is the advantge of using pointers?any increase in speed or execution time? if you know please tell what are the real time applications of data structure? thanks in...
3
5741
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
10
3682
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
2
2112
by: jodyblau | last post by:
I have noticed that when I move my database onto a different computer, I often get a message about some reference missing. So I go into the reference list, and find the one that says "Missing,"...
75
5368
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
35
1868
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
5
1249
by: campyhapper | last post by:
I've seen this in C++, not Objective C: namespace bar { struct foo { foo (int a, int b) : int a_, int b_ {} }; }
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.