Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Assigning reference to variables..

Reply
Thread Tools

Assigning reference to variables..

 
 
fAbs
Guest
Posts: n/a
 
      10-01-2003
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?

--


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-01-2003
"fAbs" <(E-Mail Removed)> wrote in message
news:3f7a760d$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
fAbs
Guest
Posts: n/a
 
      10-01-2003
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" <(E-Mail Removed)> wrote in message
news:%cveb.11035$(E-Mail Removed) link.net...
> "fAbs" <(E-Mail Removed)> wrote in message
> news:3f7a760d$(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-01-2003
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.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-01-2003

"fAbs" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      10-02-2003
"Kevin Goodsell" <(E-Mail Removed)> wrote in message
news:AcEeb.11357$(E-Mail Removed) 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


 
Reply With Quote
 
Attila Feher
Guest
Posts: n/a
 
      10-02-2003
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


 
Reply With Quote
 
Alexander Terekhov
Guest
Posts: n/a
 
      10-02-2003

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.
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-02-2003

"Attila Feher" <(E-Mail Removed)> wrote in message
news:blh8nf$505$(E-Mail Removed)...
> 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



 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      10-02-2003

"Howard" <(E-Mail Removed)> wrote in message
news:blheu5$(E-Mail Removed)...
>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assigning to the reference returned by the vector [] operator crashesthe program! arghh Daniel C++ 7 02-23-2007 01:47 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
assigning reference to a returned value from function. ypjofficial@indiatimes.com C++ 12 08-10-2006 06:33 AM
Assigning the address returned from new to a reference Protoman C++ 14 11-27-2005 08:22 AM
Assigning to a reference Dave C++ 5 11-06-2003 07:17 PM



Advertisments