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. - #include <iostream>
-
#include <cstring>
-
using namespace std;
-
-
class person
-
{
-
public:
-
-
char *name;
-
int age;
-
-
person (char *n = "no name", int a = 0)
-
{
-
name = new char[100];
-
strcpy (name, n);
-
age = a;
-
}
-
-
person (person &s) // The COPY CONSTRUCTOR
-
{
-
name = new char[100];
-
strcpy (name, s.name);
-
age = s.age;
-
}
-
-
~person ()
-
{
-
delete [] name;
-
}
-
};
-
-
void foo(person p)
-
{
-
cout << p.name << ", age " << p.age << endl << endl;
-
}
-
-
person &bar()
-
{
-
return person("Todd", 15);
-
}
-
-
int main ()
-
{
-
person k ("John", 56);
-
cout << k.name << ", age " << k.age << endl << endl;
-
-
foo(k);
-
-
person m(k);
-
-
person n = bar();
-
foo (n);
-
-
return 0;
-
}
-
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
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
So is it "by chance" that i get John?
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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 =...
|
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)
|
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...
|
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...
| |
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...
|
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,"...
|
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...
|
by: rebeccatre |
last post by:
hi can Variant archiving setTimout('.. capability be done without
using it? :-)
|
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_
{}
};
}
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |