Difference between variable declarations...

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

    Difference between variable declarations...

    Hi all,

    What's the difference between:
    int& i;

    and

    int i;

    ?

    Cheers,

    Paulo Matos

  • Rolf Magnus

    #2
    Re: Difference between variable declarations...

    pmatos wrote:
    [color=blue]
    > Hi all,
    >
    > What's the difference between:
    > int& i;
    >
    > and
    >
    > int i;
    >
    > ?[/color]

    The former is a reference to an integer, the latter is an integer.

    Comment

    • pmatos

      #3
      Re: Difference between variable declarations...

      Got it, thanks. 5.5 of C++PL. :)

      Comment

      • Sharad Kala

        #4
        Re: Difference between variable declarations...


        "Rolf Magnus" <ramagnus@t-online.de> wrote in message
        news:d19ci5$avi $02$1@news.t-online.com...[color=blue]
        > pmatos wrote:
        >[color=green]
        > > Hi all,
        > >
        > > What's the difference between:
        > > int& i;
        > >
        > > and
        > >
        > > int i;
        > >
        > > ?[/color]
        >
        > The former is a reference to an integer, the latter is an integer.[/color]

        Also former is not legal C++, a reference has to be initialized for sure.

        Sharad


        Comment

        • Victor Bazarov

          #5
          Re: Difference between variable declarations...

          Sharad Kala wrote:[color=blue]
          > "Rolf Magnus" <ramagnus@t-online.de> wrote in message
          > news:d19ci5$avi $02$1@news.t-online.com...
          >[color=green]
          >>pmatos wrote:
          >>
          >>[color=darkred]
          >>>Hi all,
          >>>
          >>>What's the difference between:
          >>>int& i;
          >>>
          >>>and
          >>>
          >>>int i;
          >>>
          >>>?[/color]
          >>
          >>The former is a reference to an integer, the latter is an integer.[/color]
          >
          >
          > Also former is not legal C++, a reference has to be initialized for sure.[/color]

          It us legal if it appears inside a class definition.

          V

          Comment

          • Sharad Kala

            #6
            Re: Difference between variable declarations...


            "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message[color=blue][color=green]
            > > Also former is not legal C++, a reference has to be initialized for[/color][/color]
            sure.[color=blue]
            >
            > It us legal if it appears inside a class definition.[/color]

            Yes.
            To OP: Even then you need to bind the reference in the initializer list.

            Sharad


            Comment

            • Rolf Magnus

              #7
              Re: Difference between variable declarations...

              Sharad Kala wrote:
              [color=blue]
              >
              > "Rolf Magnus" <ramagnus@t-online.de> wrote in message
              > news:d19ci5$avi $02$1@news.t-online.com...[color=green]
              >> pmatos wrote:
              >>[color=darkred]
              >> > Hi all,
              >> >
              >> > What's the difference between:
              >> > int& i;
              >> >
              >> > and
              >> >
              >> > int i;
              >> >
              >> > ?[/color]
              >>
              >> The former is a reference to an integer, the latter is an integer.[/color]
              >
              > Also former is not legal C++,[/color]

              Wrong. The following should compile:

              struct X
              {
              X(int& i) : i(i) {}

              int& i;
              };

              int main()
              {
              int i = 3;
              X x(i);
              }
              [color=blue]
              > a reference has to be initialized for sure.[/color]

              True.

              Comment

              • Sharad Kala

                #8
                Re: Difference between variable declarations...


                "Rolf Magnus" <ramagnus@t-online.de> .[color=blue][color=green]
                > >
                > > Also former is not legal C++,[/color]
                >
                > Wrong.[/color]

                Not totally wrong.
                int main()
                {
                int& i; // Not legal
                }

                Given the context in which OP asked the question, it seemed that it was not
                in a class definition. The point I was trying to stress was that you cannot
                leave the reference uninitialized.

                Sharad


                Comment

                • Rolf Magnus

                  #9
                  Re: Difference between variable declarations...

                  Sharad Kala wrote:
                  [color=blue]
                  >
                  > "Rolf Magnus" <ramagnus@t-online.de> .[color=green][color=darkred]
                  >> >
                  >> > Also former is not legal C++,[/color]
                  >>
                  >> Wrong.[/color]
                  >
                  > Not totally wrong.
                  > int main()
                  > {
                  > int& i; // Not legal
                  > }[/color]

                  Well, can you name one language construct that can be used _everywhere_ in a
                  program?
                  [color=blue]
                  > Given the context in which OP asked the question, it seemed that it was
                  > not in a class definition.[/color]

                  Actually, I don't see any context.
                  [color=blue]
                  > The point I was trying to stress was that you cannot leave the reference
                  > uninitialized.[/color]

                  And I agreed to that.

                  Comment

                  • Alf P. Steinbach

                    #10
                    Re: Difference between variable declarations...

                    * Rolf Magnus:[color=blue]
                    >
                    > Actually, I don't see any context.[/color]

                    Note the semicolon at the end in the Original Posting:

                    int& i;

                    This is only valid in a class definition.

                    But since the question was what does it mean, it probably doesn't
                    imply very much about context...

                    --
                    A: Because it messes up the order in which people normally read text.
                    Q: Why is it such a bad thing?
                    A: Top-posting.
                    Q: What is the most annoying thing on usenet and in e-mail?

                    Comment

                    • pmatos

                      #11
                      Re: Difference between variable declarations...


                      Alf P. Steinbach wrote:[color=blue]
                      > * Rolf Magnus:[color=green]
                      > >
                      > > Actually, I don't see any context.[/color]
                      >
                      > Note the semicolon at the end in the Original Posting:
                      >
                      > int& i;
                      >
                      > This is only valid in a class definition.
                      >
                      > But since the question was what does it mean, it probably doesn't
                      > imply very much about context...
                      >[/color]

                      Indeed, I didn't want to know about context specific questions but I
                      didn't know it had to be initialized until before reading section 5.5
                      of C++PL. Anyway, I already understood what it is for. :)

                      Thanks a lot people and don't get mad about context specific
                      questions... :)

                      Cheers,

                      Paulo Matos
                      [color=blue]
                      > --
                      > A: Because it messes up the order in which people normally read text.
                      > Q: Why is it such a bad thing?
                      > A: Top-posting.
                      > Q: What is the most annoying thing on usenet and in e-mail?[/color]

                      Comment

                      • Rolf Magnus

                        #12
                        Re: Difference between variable declarations...

                        pmatos wrote:
                        [color=blue]
                        > Thanks a lot people and don't get mad about context specific
                        > questions... :)[/color]

                        Nah, we don't. We're just nitpicking. That's what makes us nerds.
                        :-)

                        Comment

                        Working...