473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reference...

As references are behaving like a constant pointer...And we can not
change the value of reference once set...then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class abc
{
public:
int x;
public:
abc()
{
}
abc(int y)
{
x=y;
}
/*void assign(int y)
{
x=y;
} */
};

int main()
{
abc o1(10),o2(20);
abc &r=o1;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
cout<<r.x;
getch();
return 0;
}

Jun 10 '07 #1
4 1584
Hi,

It is not rereferenced. Instead the values of O2 are copied into O1. This is
done by the compiler supplied operator= (since you didn't specify your own
version of =).

So in the end you have two references pointing to two objects with the same
values (instead of two references pointing to the same object).
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Shraddha" <sh*************@gmail.comwrote in message
news:11*********************@z28g2000prd.googlegro ups.com...
As references are behaving like a constant pointer...And we can not
change the value of reference once set...then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class abc
{
public:
int x;
public:
abc()
{
}
abc(int y)
{
x=y;
}
/*void assign(int y)
{
x=y;
} */
};

int main()
{
abc o1(10),o2(20);
abc &r=o1;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
cout<<r.x;
getch();
return 0;
}

Jun 10 '07 #2
Shraddha wrote:
As references are behaving like a constant pointer...And we can not
change the value of reference once set...then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class abc
{
public:
int x;
public:
abc()
{
}
abc(int y)
{
x=y;
}
/*void assign(int y)
{
x=y;
} */
};

int main()
{
abc o1(10),o2(20);
abc &r=o1;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
cout<<r.x;
getch();
return 0;
}
Think about references as giving a new name to a variable. In your
example, the same variable can be referred to as r or o1. In fact you
are changing o1 to to value of o2.
Jun 10 '07 #3
On 2007-06-10 15:10, Shraddha wrote:
As references are behaving like a constant pointer...And we can not
change the value of reference once set...then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....
Not quite, or I'm misunderstanding you, you can not change what object
the reference refers to, but you can change the object (unless it's a
const reference).
#include<stdio.h>
You don't need this one
#include<iostream.h>
Use <iostream(notice the lack of .h) instead, using .h is non- standard.
#include<conio.h>
Non-standard header, please try not to post code that depends on non-
standard headers/libraries to this newsgroup.
class abc
{
public:
int x;
public:
abc()
{
}
abc(int y)
{
x=y;
}
/*void assign(int y)
{
x=y;
} */
};

int main()
{
abc o1(10),o2(20);
abc &r=o1;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
cout<<r.x;
getch();
return 0;
}

Try adding cout << o1.x before getch() and you'll see what happened.
Even though you commented out the assignment operator the compiler
generated one for you and that one is called when you do r = o2. So the
reference still refer to o1, but the value of o1.x has changed.

--
Erik Wikström
Jun 10 '07 #4
On Jun 10, 3:10 pm, Shraddha <shraddhajosh...@gmail.comwrote:
As references are behaving like a constant pointer...
The behave very, very much like a constant pointer that is
implicitly dereferenced with every use (at least in an
expression).
And we can not change the value of reference once set...
You cannot change what the reference refers to. Once
initialized, the reference IS what it refers to, the value of
the reference is the value of what it refers to, and you can
certainly change that.
then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....
Let's rework it converting all of the uses of a reference into
the corresponding uses of a pointer:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class abc
{
public:
int x;
public:
abc()
{}
abc(int y)
{
x=y;}
/*void assign(int y)
{
x=y;
} */
};
int main()
{
abc o1(10),o2(20);
abc &r=o1;
abc*const r = &o1 ;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
*r = o2 ;
cout<<r.x;
getch();
return 0;
}
BTW: my systems don't have a header <conio.h>; to get clrscr()
and getch(), I have to include <termcap.h>, which is a
non-standard (but very wide spread) extention. When posting
code here, it's better to forget them.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.