variable declaration

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

    #1

    variable declaration

    Hello,

    I would like to know the difference between two variable declaration
    ways:

    int point(0);

    and

    int point=0;

    Thanks for your help!!

    A.

  • Tomás

    #2
    Re: variable declaration

    widmont posted:
    [color=blue]
    > Hello,
    >
    > I would like to know the difference between two variable declaration
    > ways:
    >
    > int point(0);
    >
    > and
    >
    > int point=0;
    >
    > Thanks for your help!!
    >
    > A.[/color]

    When you're working with intrinsic types, there's no difference whatsoever.
    It's a situation of "Thanks" Vs. "Thank you". Pick whichever tickles your
    fancy.

    Be careful though. If you want to "default initialise" something, then you
    can't simply write:

    int point();

    That my friend is a function declaration. You can work your way around this
    with:

    int point = int();

    But unfortunately, the type you're working with must have a public copy
    constructor. So it won't work with "ostringstream" :

    ostringstream blah = ostringstream() ;


    Fortunately though, if your type is a POD, you can do this:

    PodType object = {};

    (I'm open to correction on whether the above actually zero initializes
    everything. If memory serves me right, then it does.)

    When you have a contructor which takes more than one argument, then you
    *have* to use the parenthesis form, eg.:

    Dog benji("Benji",7 );

    You can't do:

    Dog benji = "Benji", 7;


    If you want to know how to default initialise something that has a non-
    public copy constructor, then:

    template<class T>
    class DefaultInitiali sed : public T
    {

    DefaultInitiali sed() : T()
    };

    int main()
    {
    DefaultInitiali sed<ostringstre am> k;
    }


    -Tomás

    Comment

    • Tomás

      #3
      Re: variable declaration

      [color=blue]
      > template<class T>
      > class DefaultInitiali sed : public T
      > {
      >
      > DefaultInitiali sed() : T()[/color]

      {

      }
      [color=blue]
      > };
      >
      > int main()
      > {
      > DefaultInitiali sed<ostringstre am> k;
      > }[/color]

      Comment

      • Victor Bazarov

        #4
        Re: variable declaration

        widmont wrote:[color=blue]
        > I would like to know the difference between two variable declaration
        > ways:
        >
        > int point(0);
        >
        > and
        >
        > int point=0;[/color]

        In this particular case (with 'int'), none.

        V
        --
        Please remove capital As from my address when replying by mail


        Comment

        • Rolf Magnus

          #5
          Re: variable declaration

          Tomás wrote:
          [color=blue]
          > widmont posted:
          >[color=green]
          >> Hello,
          >>
          >> I would like to know the difference between two variable declaration
          >> ways:
          >>
          >> int point(0);
          >>
          >> and
          >>
          >> int point=0;
          >>
          >> Thanks for your help!!
          >>
          >> A.[/color]
          >
          > When you're working with intrinsic types, there's no difference
          > whatsoever. It's a situation of "Thanks" Vs. "Thank you". Pick whichever
          > tickles your fancy.
          >
          > Be careful though. If you want to "default initialise" something, then you
          > can't simply write:
          >
          > int point();
          >
          > That my friend is a function declaration. You can work your way around
          > this with:
          >
          > int point = int();
          >
          > But unfortunately, the type you're working with must have a public copy
          > constructor. So it won't work with "ostringstream" :
          >
          > ostringstream blah = ostringstream() ;[/color]

          No, but you can simply do:

          ostringstream blah;

          This will give you a default initialized variable, if the type is non-POD.
          [color=blue]
          > Fortunately though, if your type is a POD, you can do this:
          >
          > PodType object = {};[/color]

          I think this only works for compound types.
          [color=blue]
          > (I'm open to correction on whether the above actually zero initializes
          > everything. If memory serves me right, then it does.)[/color]

          It default-initializes everything, which means zero for integer types.
          [color=blue]
          > When you have a contructor which takes more than one argument, then you
          > *have* to use the parenthesis form, eg.:
          >
          > Dog benji("Benji",7 );
          >
          > You can't do:
          >
          > Dog benji = "Benji", 7;
          >
          >
          > If you want to know how to default initialise something that has a non-
          > public copy constructor, then:
          >
          > template<class T>
          > class DefaultInitiali sed : public T
          > {
          >
          > DefaultInitiali sed() : T()
          > };[/color]

          That constructor is private.
          [color=blue]
          > int main()
          > {
          > DefaultInitiali sed<ostringstre am> k;
          > }[/color]

          Comment

          • Marcus Kwok

            #6
            Re: variable declaration

            "Tom?s" <NULL@null.null > wrote:[color=blue]
            > When you have a contructor which takes more than one argument, then you
            > *have* to use the parenthesis form, eg.:
            >
            > Dog benji("Benji",7 );
            >
            > You can't do:
            >
            > Dog benji = "Benji", 7;[/color]

            However, you can do:

            Dog benji = Dog("Benji", 7);

            (assuming the appropriate copy-constructors, etc., are accessible).

            --
            Marcus Kwok

            Comment

            Working...