Assigning reference to variables..

Discussion in 'C++' started by fAbs, Oct 1, 2003.

  1. fAbs

    fAbs Guest

    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?

    --
    fAbs, Oct 1, 2003
    #1
    1. Advertising

  2. fAbs

    Mike Wahler Guest

    "fAbs" <> wrote in message
    news:3f7a760d$...
    > 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
    Mike Wahler, Oct 1, 2003
    #2
    1. Advertising

  3. fAbs

    fAbs Guest

    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" <> wrote in message
    news:%cveb.11035$...
    > "fAbs" <> wrote in message
    > news:3f7a760d$...
    > > 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
    >
    >
    fAbs, Oct 1, 2003
    #3
  4. 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.
    Kevin Goodsell, Oct 1, 2003
    #4
  5. fAbs

    Howard Guest

    "fAbs" <> wrote in message
    news:...
    > 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
    Howard, Oct 1, 2003
    #5
  6. "Kevin Goodsell" <> wrote in message
    news:AcEeb.11357$...
    > 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
    Gary Labowitz, Oct 2, 2003
    #6
  7. fAbs

    Attila Feher Guest

    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
    Attila Feher, Oct 2, 2003
    #7
  8. 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.
    Alexander Terekhov, Oct 2, 2003
    #8
  9. fAbs

    Howard Guest

    "Attila Feher" <> wrote in message
    news:blh8nf$505$...
    > 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
    Howard, Oct 2, 2003
    #9
  10. fAbs

    jeffc Guest

    "Howard" <> wrote in message
    news:blheu5$...
    >
    > 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.
    jeffc, Oct 2, 2003
    #10
  11. "Howard" <> wrote...
    >
    > "Attila Feher" <> wrote in message
    > news:blh8nf$505$...
    > > 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?
    Victor Bazarov, Oct 2, 2003
    #11
  12. fAbs

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:hHXeb.667172$uu5.108805@sccrnsc04...
    > "Howard" <> 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
    Howard, Oct 2, 2003
    #12
  13. fAbs

    Jerry Coffin Guest

    In article <blheu5$>,
    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.
    Jerry Coffin, Oct 2, 2003
    #13
  14. fAbs

    WW Guest

    jeffc wrote:
    > "Howard" <> wrote in message
    > news:blheu5$...
    >>
    >> 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
    WW, Oct 2, 2003
    #14
  15. fAbs

    WW Guest

    Howard wrote:
    > "Victor Bazarov" <> wrote in message
    > news:hHXeb.667172$uu5.108805@sccrnsc04...
    >> "Howard" <> 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
    WW, Oct 2, 2003
    #15
  16. fAbs

    WW Guest

    Howard wrote:
    > "Attila Feher" <> wrote in message
    > news:blh8nf$505$...
    >> 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
    WW, Oct 2, 2003
    #16
  17. Gary Labowitz wrote:

    > "Kevin Goodsell" <> wrote in message
    > news:AcEeb.11357$...
    >
    >>
    >>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.
    Kevin Goodsell, Oct 2, 2003
    #17
  18. fAbs

    WW Guest

    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
    WW, Oct 2, 2003
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave

    Assigning to a reference

    Dave, Nov 6, 2003, in forum: C++
    Replies:
    5
    Views:
    366
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 6, 2003
  2. Protoman
    Replies:
    14
    Views:
    598
    n2xssvv g02gfr12930
    Nov 27, 2005
  3. Replies:
    12
    Views:
    482
  4. Daniel
    Replies:
    7
    Views:
    243
    Emmanuel Deloget
    Feb 23, 2007
  5. weston
    Replies:
    1
    Views:
    238
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page