473,800 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assigning reference to variables..

hi, when I do:
someclass &someobject = someotherobject ;

it asigns the reference of someotherobject to the variable 'someobject' that
you just declared. SO that basically
someobject and someotherobject are really the same variable with two names.

but if I do:

someclass someobject;
&someobject = someotherobject ;

it no longer works D:
does anyone know how to get it to work in the second case?

--
Jul 19 '05 #1
17 4708
"fAbs" <fA**@fAbs.fAbs > wrote in message
news:3f******** **@news.iprimus .com.au...
hi, when I do:
someclass &someobject = someotherobject ;

it asigns the reference of someotherobject to the variable 'someobject'
No.

For the above to be valid, there must exist
an object of type 'someclass' (or a type derived from it)
named 'someotherobjec t'. The statement above declares
'someobject' to be a reference to the object 'someotherobjec t'.

that
you just declared. SO that basically
someobject and someotherobject are really the same variable with two names.

'someotherobjec t' is an object of type 'someclass'.
'someobject' becomes an 'alias' for 'someotherobjec t',
i.e. both names refer to the same object.

Critical point: A reference, when declared *must*
be bound to an object. E.g. you cannot write:

someclass& someobject;

Also, once a reference has been delcared, it *cannot*
later be bound to a different object. It stays being
an 'alias' for the object it was initialized with
for its entire lifetime.

but if I do:

someclass someobject;
&someobject = someotherobject ;
This is not legal.

it no longer works D:
does anyone know how to get it to work in the second case?


It's not allowed.

Also, typically the use of references is restricted
to function parameters and return types, and sometimes
as class members.

What specifically do you want to do?

Tell us that, and we'll tell you how.

BTW which C++ book(s) are you reading?

-Mike
Jul 19 '05 #2
thanks.
IM not really reading any.
I got this information from lecture notes.

I have a class that I want to change such that isntead of editing its
variables, it edits the variables in a stuct that is pased to it in its
contructor.
because I couldnt be bothered rewriting all the code so it uses the
variables in the struct I thought i would just make the variales in the code
refer to the variables in the struct.

--
" 'Religion' is just another word for 'Mainstream Cult' "

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:%c******** **********@news read3.news.pas. earthlink.net.. .
"fAbs" <fA**@fAbs.fAbs > wrote in message
news:3f******** **@news.iprimus .com.au...
hi, when I do:
someclass &someobject = someotherobject ;

it asigns the reference of someotherobject to the variable 'someobject'


No.

For the above to be valid, there must exist
an object of type 'someclass' (or a type derived from it)
named 'someotherobjec t'. The statement above declares
'someobject' to be a reference to the object 'someotherobjec t'.

that
you just declared. SO that basically
someobject and someotherobject are really the same variable with two

names.

'someotherobjec t' is an object of type 'someclass'.
'someobject' becomes an 'alias' for 'someotherobjec t',
i.e. both names refer to the same object.

Critical point: A reference, when declared *must*
be bound to an object. E.g. you cannot write:

someclass& someobject;

Also, once a reference has been delcared, it *cannot*
later be bound to a different object. It stays being
an 'alias' for the object it was initialized with
for its entire lifetime.

but if I do:

someclass someobject;
&someobject = someotherobject ;


This is not legal.

it no longer works D:
does anyone know how to get it to work in the second case?


It's not allowed.

Also, typically the use of references is restricted
to function parameters and return types, and sometimes
as class members.

What specifically do you want to do?

Tell us that, and we'll tell you how.

BTW which C++ book(s) are you reading?

-Mike

Jul 19 '05 #3
fAbs wrote:
thanks.
Please don't top-post. Read section 5 of the FAQ for posting guidelines:

http://www.parashift.com/c++-faq-lite/
IM not really reading any.
I got this information from lecture notes.


That's not a very good way to learn C++. Frankly, most C++ teachers
don't know the language well enough to be teaching it (but they usually
believe they do). A good book is about the best way.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

"fAbs" <fA**@fAbs.fAbs > wrote in message
news:3f******** @news.iprimus.c om.au...
thanks.
IM not really reading any.
I got this information from lecture notes.

I have a class that I want to change such that isntead of editing its
variables, it edits the variables in a stuct that is pased to it in its
contructor.
because I couldnt be bothered rewriting all the code so it uses the
variables in the struct I thought i would just make the variales in the code refer to the variables in the struct.

--


Why not use a pointer variable instead of a reference? You can make a
pointer variable point to any object. (in other words, you can re-assign
it). With references, you can't do that because they are bound to the same
object instance for their lifetime. (Just be sure it points to a valid
object before you dereference it!)
-Howard
Jul 19 '05 #5
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:Ac******** **********@news read3.news.pas. earthlink.net.. .
fAbs wrote:
thanks.


Please don't top-post. Read section 5 of the FAQ for posting guidelines:

http://www.parashift.com/c++-faq-lite/
IM not really reading any.
I got this information from lecture notes.


That's not a very good way to learn C++. Frankly, most C++ teachers
don't know the language well enough to be teaching it (but they usually
believe they do). A good book is about the best way.


Ahem. I teach C++ AND I know it well enough to be teaching it. BUT I lurk
here to learn more.
What I have learned is that I have to teach it wrong first to get ideas
across and then correct what is wrong once students CAN get it right.
Example: C-strings vs. string class. (Or for that matter .h file headers vs.
headers.) To jump into the header usage brings up namespaces and that
requires some feeling for variable allocation. And sometimes students don't
know what a variable is.
There may a good book out there, but I still haven't found a good TEXT,
which a different animal. And the texts used are usually dictated for use by
some committee who is more swayed by the rep (hey! free lunch) than the
contents of the book.
I am currently using a book which is pretty good, but non-standard. Every
example in the book is void main( ), so I teach int main( ) and tell the
student they get no credit for any homework that uses void main( ). Can you
guess what comes in for homework? You bet, void main( ).
With that mindset and low ability to follow instructions I'd be crazy to try
and teach namespaces right off the bat.
I'd much rather prefer they use their lecture notes. Or even take lecture
notes. Or take them down correctly.

The only thing worse than teaching C++ is working next to a guy programming
it with void main( ) and arguing all the time.
--
Gary
Jul 19 '05 #6
Gary Labowitz wrote:
The only thing worse than teaching C++ is working next to a guy
programming it with void main( ) and arguing all the time.


The human stupidity is the only endless resource on Earth.

--
Attila aka WW
Jul 19 '05 #7

Attila Feher wrote:
[...]
The human stupidity is the only endless resource on Earth.


"Anything that begins well, ends badly.
Anything that begins badly, ends worse."

regards,
alexander.
Jul 19 '05 #8

"Attila Feher" <at**********@l mf.ericsson.se> wrote in message
news:bl******** **@newstree.wis e.edt.ericsson. se...
Gary Labowitz wrote:
The only thing worse than teaching C++ is working next to a guy
programming it with void main( ) and arguing all the time.


The human stupidity is the only endless resource on Earth.

--
Attila aka WW


At least we can take heart in the fact that only about half the people on
earth are of below average intelligence!

(think about it...)

-Howard

Jul 19 '05 #9

"Howard" <al*****@hotmai l.com> wrote in message
news:bl******** @dispatch.conce ntric.net...

At least we can take heart in the fact that only about half the people on
earth are of below average intelligence!

(think about it...)


Depends on how you define average. When you factor intelligence like mine
into the mean, probably about 99% of the people are of below average
intelligence.
Jul 19 '05 #10

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

Similar topics

4
4959
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers to. My question is: In what situations is it better to use a reference variable over a pointer? Also, are there other differences besides the ones I listed above?
12
1904
by: ypjofficial | last post by:
Hello All, I need to return a vector from a function.The vector is of int and it can contain as many as 1000 elements.Earlier I was doing //function definition vector<intretIntVector() { vector<intvecInt; /* //Populate the vector with int values.
5
1629
by: Chaos | last post by:
Is It possible to have reference variables like in PHP ex. <?php $x = 1; $y =& $x; $y += 1;
16
2393
by: jryden | last post by:
I can do this: struct myInt { int i; }; void func1(myInt &i = myInt()); but not this:
3
1522
by: v4vijayakumar | last post by:
While we can do whatever we want to do with the member variables, with the member references, why member references are not common? Note: This is not homework question. :)
1
1301
by: localhost1 | last post by:
i want to make a loop in javascript code to assign the php variables in the javascript variables. can it be done? how?
3
1410
by: Rahul | last post by:
Hi Everyone, I just tried the following code, in response to some post on reference member variable and i'm not able to understand what exactly is happening over here, int main() { int a=5,b=6;
9
2340
by: sulekhasweety | last post by:
dear all, can anyone explain the differences between pointer variable and reference variables ?
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.