Assigning reference to variables..

F

fAbs

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?

--
 
M

Mike Wahler

fAbs said:
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
 
F

fAbs

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

Howard

fAbs said:
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
 
G

Gary Labowitz

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

http://www.parashift.com/c++-faq-lite/


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

Attila Feher

Gary said:
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.
 
A

Alexander Terekhov

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

Howard

Attila Feher said:
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!

(think about it...)

-Howard
 
J

jeffc

Howard said:
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.
 
V

Victor Bazarov

Howard said:
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?
 
H

Howard

Victor Bazarov said:
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
 
J

Jerry Coffin

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

WW

jeffc said:
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". ;-)
 
W

WW

Howard said:
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.
 
K

Kevin Goodsell

Gary said:
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
 
W

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top