Assigning reference to variables..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fAbs

    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?

    --


  • Mike Wahler

    #2
    Re: Assigning reference to variables..

    "fAbs" <fAbs@fAbs.fAbs > wrote in message
    news:3f7a760d$1 _1@news.iprimus .com.au...[color=blue]
    > hi, when I do:
    > someclass &someobject = someotherobject ;
    >
    > it asigns the reference of someotherobject to the variable 'someobject'[/color]

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

    [color=blue]
    >that
    > you just declared. SO that basically
    > someobject and someotherobject are really the same variable with two[/color]
    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.
    [color=blue]
    >
    > but if I do:
    >
    > someclass someobject;
    > &someobject = someotherobject ;[/color]

    This is not legal.
    [color=blue]
    >
    > it no longer works D:
    > does anyone know how to get it to work in the second case?[/color]

    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


    Comment

    • fAbs

      #3
      Re: Assigning reference to variables..

      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" <mkwahler@mkwah ler.net> wrote in message
      news:%cveb.1103 5$NX3.5711@news read3.news.pas. earthlink.net.. .[color=blue]
      > "fAbs" <fAbs@fAbs.fAbs > wrote in message
      > news:3f7a760d$1 _1@news.iprimus .com.au...[color=green]
      > > hi, when I do:
      > > someclass &someobject = someotherobject ;
      > >
      > > it asigns the reference of someotherobject to the variable 'someobject'[/color]
      >
      > 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'.
      >
      >[color=green]
      > >that
      > > you just declared. SO that basically
      > > someobject and someotherobject are really the same variable with two[/color]
      > 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.
      >[color=green]
      > >
      > > but if I do:
      > >
      > > someclass someobject;
      > > &someobject = someotherobject ;[/color]
      >
      > This is not legal.
      >[color=green]
      > >
      > > it no longer works D:
      > > does anyone know how to get it to work in the second case?[/color]
      >
      > 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
      >
      >[/color]


      Comment

      • Kevin Goodsell

        #4
        Re: Assigning reference to variables..

        fAbs wrote:[color=blue]
        > thanks.[/color]

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


        [color=blue]
        > IM not really reading any.
        > I got this information from lecture notes.[/color]

        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.

        Comment

        • Howard

          #5
          Re: Assigning reference to variables..


          "fAbs" <fAbs@fAbs.fAbs > wrote in message
          news:3f7a93fe_1 @news.iprimus.c om.au...[color=blue]
          > 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[/color]
          code[color=blue]
          > refer to the variables in the struct.
          >
          > --[/color]

          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


          Comment

          • Gary Labowitz

            #6
            Re: Assigning reference to variables..

            "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
            news:AcEeb.1135 7$NX3.7639@news read3.news.pas. earthlink.net.. .[color=blue]
            > fAbs wrote:[color=green]
            > > thanks.[/color]
            >
            > Please don't top-post. Read section 5 of the FAQ for posting guidelines:
            >
            > http://www.parashift.com/c++-faq-lite/
            >[color=green]
            > > IM not really reading any.
            > > I got this information from lecture notes.[/color]
            >
            > 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.[/color]

            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


            Comment

            • Attila Feher

              #7
              Re: Assigning reference to variables..

              Gary Labowitz wrote:[color=blue]
              > The only thing worse than teaching C++ is working next to a guy
              > programming it with void main( ) and arguing all the time.[/color]

              The human stupidity is the only endless resource on Earth.

              --
              Attila aka WW


              Comment

              • Alexander Terekhov

                #8
                Re: Assigning reference to variables..


                Attila Feher wrote:
                [...][color=blue]
                > The human stupidity is the only endless resource on Earth.[/color]

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

                regards,
                alexander.

                Comment

                • Howard

                  #9
                  Re: Assigning reference to variables..


                  "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                  news:blh8nf$505 $1@newstree.wis e.edt.ericsson. se...[color=blue]
                  > Gary Labowitz wrote:[color=green]
                  > > The only thing worse than teaching C++ is working next to a guy
                  > > programming it with void main( ) and arguing all the time.[/color]
                  >
                  > The human stupidity is the only endless resource on Earth.
                  >
                  > --
                  > Attila aka WW
                  >
                  >[/color]

                  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



                  Comment

                  • jeffc

                    #10
                    Re: Assigning reference to variables..


                    "Howard" <alicebt@hotmai l.com> wrote in message
                    news:blheu5$15p @dispatch.conce ntric.net...[color=blue]
                    >
                    > 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...)[/color]

                    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.


                    Comment

                    • Victor Bazarov

                      #11
                      Re: Assigning reference to variables..

                      "Howard" <alicebt@hotmai l.com> wrote...[color=blue]
                      >
                      > "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                      > news:blh8nf$505 $1@newstree.wis e.edt.ericsson. se...[color=green]
                      > > Gary Labowitz wrote:[color=darkred]
                      > > > The only thing worse than teaching C++ is working next to a guy
                      > > > programming it with void main( ) and arguing all the time.[/color]
                      > >
                      > > The human stupidity is the only endless resource on Earth.
                      > >
                      > > --
                      > > Attila aka WW
                      > >
                      > >[/color]
                      >
                      > 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...)[/color]

                      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
                      "mathematic al 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?


                      Comment

                      • Howard

                        #12
                        Re: Assigning reference to variables..


                        "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
                        news:hHXeb.6671 72$uu5.108805@s ccrnsc04...[color=blue]
                        > "Howard" <alicebt@hotmai l.com> wrote...[color=green]
                        > >
                        > >
                        > >
                        > > At least we can take heart in the fact that only about half the people[/color][/color]
                        on[color=blue][color=green]
                        > > earth are of below average intelligence!
                        > >
                        > > (think about it...)[/color]
                        >
                        > It depends on how you calculate the average intelligence and on the spread
                        > of intelligence among people.
                        >[/color]

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

                        -Howard


                        Comment

                        • Jerry Coffin

                          #13
                          Re: Assigning reference to variables..

                          In article <blheu5$15p@dis patch.concentri c.net>, alicebt@hotmail .com
                          says...

                          [ ... ]
                          [color=blue]
                          > 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...)[/color]

                          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.

                          Comment

                          • WW

                            #14
                            Re: Assigning reference to variables..

                            jeffc wrote:[color=blue]
                            > "Howard" <alicebt@hotmai l.com> wrote in message
                            > news:blheu5$15p @dispatch.conce ntric.net...[color=green]
                            >>
                            >> 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...)[/color]
                            >
                            > 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.[/color]

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

                            --
                            WW aka Attila


                            Comment

                            • WW

                              #15
                              Re: Assigning reference to variables..

                              Howard wrote:[color=blue]
                              > "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
                              > news:hHXeb.6671 72$uu5.108805@s ccrnsc04...[color=green]
                              >> "Howard" <alicebt@hotmai l.com> wrote...[color=darkred]
                              >>>
                              > > >
                              >>>
                              >>> 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...)[/color]
                              >>
                              >> It depends on how you calculate the average intelligence and on the
                              >> spread of intelligence among people.
                              >>[/color]
                              >
                              > Well, just like on TV, I define "average" in the way that suits my
                              > point best! In this case, "median" instead of "mean". :-)[/color]

                              That's mean. :-)

                              --
                              WW aka Attila


                              Comment

                              Working...