what is the difference ? (pass by reference)

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

    what is the difference ? (pass by reference)

    Hello,

    could someone tell me what is the difference between:


    1)

    int *data;

    void doSomething(*da ta)
    {

    }



    2)
    int data;
    void doSomething(&da ta)
    {

    }



    they both pass data by reference right?
    Or not?

    Thank you
    Vasileios
  • Nils Petter Vaskinn

    #2
    Re: what is the difference ? (pass by reference)

    On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:

    [color=blue]
    > void doSomething(*da ta)
    > void doSomething(&da ta)[/color]
    [color=blue]
    > they both pass data by reference right?[/color]

    No, one passes data by reference the other passes a reference to the data.

    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up

    Comment

    • Grumble

      #3
      Re: what is the difference ? (pass by reference)

      Vasileios wrote:[color=blue]
      > could someone tell me what is the difference between:[/color]

      Does this help?


      Comment

      • Jon Bell

        #4
        Re: what is the difference ? (pass by reference)

        In article <40d3a3ec.03112 70452.13ce1851@ posting.google. com>,
        Vasileios <vasileios@zogr afos.org> wrote:[color=blue]
        >
        >1)
        >
        >int *data;
        >
        >void doSomething(*da ta)
        >{
        >
        >}
        >
        >
        >
        >2)
        >int data;
        >void doSomething(&da ta)
        >{
        >
        >}
        >
        >
        >
        >they both pass data by reference right?
        >Or not?[/color]

        Not. They're both invalid syntax.

        --
        Jon Bell <jtbellap8@pres by.edu> Presbyterian College
        Dept. of Physics and Computer Science Clinton, South Carolina USA

        Comment

        • Vasileios

          #5
          Re: what is the difference ? (pass by reference)

          "Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message news:<pan.2003. 11.27.13.08.39. 250657@spam.for .me.invalid>...[color=blue]
          > On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:
          >
          >[color=green]
          > > void doSomething(*da ta)
          > > void doSomething(&da ta)[/color]
          >[color=green]
          > > they both pass data by reference right?[/color]
          >
          > No, one passes data by reference the other passes a reference to the data.[/color]


          Hmm... does this mean that with the first "void doSomething(*da ta)"
          any changes I make inside the function will change the data that is
          passed from outside, and the second "void doSomething(&da ta)" will
          only change the data inside the function?


          V.Z.

          Comment

          • Dave O'Hearn

            #6
            Re: what is the difference ? (pass by reference)

            vasileios@zogra fos.org (Vasileios) wrote:[color=blue]
            > Hmm... does this mean that with the first "void doSomething(*da ta)"
            > any changes I make inside the function will change the data that is
            > passed from outside, and the second "void doSomething(&da ta)" will
            > only change the data inside the function?[/color]

            It would help if you gave some info on how long you have been using
            C++. Are you familiar with pointers and trying to understand how
            references are different, or are you new to all of this stuff at once?

            --
            Dave O'Hearn

            Comment

            • friedrich

              #7
              Re: what is the difference ? (pass by reference)

              > 1)[color=blue]
              >
              > int *data;
              >
              > void doSomething(*da ta)[/color]

              This should be:

              void doSomething(int *data)

              [color=blue]
              > 2)
              > int data;
              > void doSomething(&da ta)[/color]

              This should be:

              void doSomething(int &data)

              In the first example you would have to use a pointer-to-int as the
              function argument, in the second, you could use an int as an argument
              and the function would automatically use a reference. If you used
              const:

              void doSomething(con st int &data)

              that would protect the int from being changed outside of the function.

              Your function name looks like a Java style name. Are you a Java
              programmer?

              -FA

              Comment

              • Kon Tantos

                #8
                Re: what is the difference ? (pass by reference)



                Vasileios wrote:[color=blue]
                > "Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message news:<pan.2003. 11.27.13.08.39. 250657@spam.for .me.invalid>...
                >[color=green]
                >>On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:
                >>
                >>
                >>[color=darkred]
                >>>void doSomething(*da ta)[/color][/color][/color]
                this passes a copy of a reference (called a pointer). Your function can change 'data'
                without affecting the copy in the calling code. You can dereference 'data' to change
                data in the calling code (*data = ...)
                [color=blue][color=green][color=darkred]
                >>>void doSomething(&da ta)[/color][/color][/color]
                this passes a reference to data in the calling code. When you access data you are
                _actually_ working with the outside data. So data = ... directly changes whatever
                data is 'refering to'.
                [color=blue][color=green]
                >>
                >>
                >>[color=darkred]
                >>>they both pass data by reference right?[/color]
                >>
                >>No, one passes data by reference the other passes a reference to the data.[/color]
                >
                >
                >
                > Hmm... does this mean that with the first "void doSomething(*da ta)"
                > any changes I make inside the function will change the data that is
                > passed from outside, and the second "void doSomething(&da ta)" will
                > only change the data inside the function?
                >
                >
                > V.Z.[/color]

                Comment

                • Vasileios Zografos

                  #9
                  Re: what is the difference ? (pass by reference)

                  Kon Tantos wrote:[color=blue]
                  >
                  >
                  > Vasileios wrote:
                  >[color=green]
                  >> "Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message
                  >> news:<pan.2003. 11.27.13.08.39. 250657@spam.for .me.invalid>...
                  >>[color=darkred]
                  >>> On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:
                  >>>
                  >>>
                  >>>
                  >>>> void doSomething(*da ta)[/color][/color]
                  >
                  > this passes a copy of a reference (called a pointer). Your function can
                  > change 'data' without affecting the copy in the calling code. You can
                  > dereference 'data' to change data in the calling code (*data = ...)
                  >[color=green][color=darkred]
                  >>>> void doSomething(&da ta)[/color][/color]
                  >
                  > this passes a reference to data in the calling code. When you access
                  > data you are _actually_ working with the outside data. So data = ...
                  > directly changes whatever data is 'refering to'.
                  >[color=green][color=darkred]
                  >>>
                  >>>
                  >>>
                  >>>> they both pass data by reference right?
                  >>>
                  >>>
                  >>> No, one passes data by reference the other passes a reference to the
                  >>> data.[/color]
                  >>
                  >>
                  >>
                  >>
                  >> Hmm... does this mean that with the first "void doSomething(*da ta)"
                  >> any changes I make inside the function will change the data that is
                  >> passed from outside, and the second "void doSomething(&da ta)" will
                  >> only change the data inside the function?
                  >>
                  >>
                  >> V.Z.[/color]
                  >
                  >[/color]

                  excellent. thats what I wanted to know.
                  plain and simple.

                  Thank you

                  Comment

                  Working...