473,398 Members | 2,404 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,398 software developers and data experts.

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 4677
"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 'someotherobject'. The statement above declares
'someobject' to be a reference to the object 'someotherobject'.

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

'someotherobject' is an object of type 'someclass'.
'someobject' becomes an 'alias' for 'someotherobject',
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******@mkwahler.net> wrote in message
news:%c******************@newsread3.news.pas.earth link.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 'someotherobject'. The statement above declares
'someobject' to be a reference to the object 'someotherobject'.

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

names.

'someotherobject' is an object of type 'someclass'.
'someobject' becomes an 'alias' for 'someotherobject',
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.com.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*********************@neverbox.com> wrote in message
news:Ac******************@newsread3.news.pas.earth link.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**********@lmf.ericsson.se> wrote in message
news:bl**********@newstree.wise.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*****@hotmail.com> wrote in message
news:bl********@dispatch.concentric.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
"Howard" <al*****@hotmail.com> wrote...

"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bl**********@newstree.wise.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...)


It depends on how you calculate the average intelligence and on the spread
of intelligence among people.

Take a set of numbers 1 1 1 1 1 1 2 2 2 2 2 2 8 100. The
"mathematical expectation" (the sum divided by the count) is going to be
126/14, or 9, and 13 out of 14 numbers (92.9%) would be below that average.

Think about it...

Victor

P.S. Wouldn't it be nice to be the 100 in that list?
Jul 19 '05 #11

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:hHXeb.667172$uu5.108805@sccrnsc04...
"Howard" <al*****@hotmail.com> wrote...

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...)


It depends on how you calculate the average intelligence and on the spread
of intelligence among people.


Well, just like on TV, I define "average" in the way that suits my point
best! In this case, "median" instead of "mean". :-)

-Howard
Jul 19 '05 #12
In article <bl********@dispatch.concentric.net>, al*****@hotmail.com
says...

[ ... ]
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...)


That would only be true if the "average" you use is the median instead
of the (much more common) arithmetic mean. A mean can be heavily
affected by a small group at an extreme (a typical example of this is
average income -- a few extremely rich people raise mean _well_ above
the median).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #13
WW
jeffc wrote:
"Howard" <al*****@hotmail.com> wrote in message
news:bl********@dispatch.concentric.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.


Yep. That 99% is also called "the modest people". ;-)

--
WW aka Attila
Jul 19 '05 #14
WW
Howard wrote:
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:hHXeb.667172$uu5.108805@sccrnsc04...
"Howard" <al*****@hotmail.com> wrote...

> >

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...)


It depends on how you calculate the average intelligence and on the
spread of intelligence among people.


Well, just like on TV, I define "average" in the way that suits my
point best! In this case, "median" instead of "mean". :-)


That's mean. :-)

--
WW aka Attila
Jul 19 '05 #15
WW
Howard wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bl**********@newstree.wise.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.


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


IMO the stupidity I talk about and intelligence they measure has nothing to
do with each other. IQ does not measure quality or method of the solution
one gives to a problem. It only finds out if that problem was solved.
People using void main in an environment not decent enough to warn about it
have solved the problem. They might be shortsighted and not give a damn
about the information that void main is not legal. And that might even help
them while making an IQ test. They never get distracted by anything.
Morals or consequences are beyond them. Look at our late troll.

--
WW aka Attila
Jul 19 '05 #16
Gary Labowitz wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Ac******************@newsread3.news.pas.earth link.net...

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.

<snip>

Then I wasn't talking about you. ;)

Seriously, I think it's safe to say that most teachers' C++ lecture
notes are riddled with errors, and not a good way to learn the language.
Maybe I'm wrong (about C++ teachers in general), but I don't think I am,
based on how many C++ programmers I've come across who know a language
bearing some similarities to C++, but being fundamentally a completely
different language. These same programmers are often the ones who are
quite certain that C++ is what they say it is, and will hold onto this
belief even in the face of overwhelming evidence to the contrary.

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

Jul 19 '05 #17
WW
Kevin Goodsell wrote:
[SNIP]
Seriously, I think it's safe to say that most teachers' C++ lecture
notes are riddled with errors, and not a good way to learn the
language. Maybe I'm wrong (about C++ teachers in general), but I
don't think I am, based on how many C++ programmers I've come across
who know a language bearing some similarities to C++, but being
fundamentally a completely different language. These same programmers
are often the ones who are quite certain that C++ is what they say it
is, and will hold onto this belief even in the face of overwhelming
evidence to the contrary.


In a Hungarian University they had the following name for a program:
cprog01.cpp. The program was supposed to be a C program. The first header
it loaded was conio.h. It was full of variable declarations in the middle
of compound statements. When I have made the students to point out all the
errors in the code the teacher started to rant and accused them to attack
him, because he was a refugee... The Brave New World...

--
WW aka Attila
Jul 19 '05 #18

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

Similar topics

4
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...
12
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() {...
5
by: Chaos | last post by:
Is It possible to have reference variables like in PHP ex. <?php $x = 1; $y =& $x; $y += 1;
16
by: jryden | last post by:
I can do this: struct myInt { int i; }; void func1(myInt &i = myInt()); but not this:
3
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
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
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...
9
by: sulekhasweety | last post by:
dear all, can anyone explain the differences between pointer variable and reference variables ?
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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.