Build-in types initialization

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

    Build-in types initialization

    Hi,

    void f()
    {
    int i1; // i1 now holds undefined value
    int i2 = int(); // i2 now holds 0
    }

    Is the above true?





  • Sharad Kala

    #2
    Re: Build-in types initialization


    "Marcin Kalicinski" <kalita@poczta. onet.pl> wrote in message
    news:c1i7fp$dal $1@korweta.task .gda.pl...[color=blue]
    > Hi,
    >
    > void f()
    > {
    > int i1; // i1 now holds undefined value
    > int i2 = int(); // i2 now holds 0
    > }
    >
    > Is the above true?[/color]

    Yes


    Comment

    • Sharad Kala

      #3
      Re: Build-in types initialization


      "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > wrote in message
      news:c1i8bg$1ii hqh$1@ID-221354.news.uni-berlin.de...[color=blue]
      >
      > "Marcin Kalicinski" <kalita@poczta. onet.pl> wrote in message
      > news:c1i7fp$dal $1@korweta.task .gda.pl...[color=green]
      > > Hi,
      > >
      > > void f()
      > > {
      > > int i1; // i1 now holds undefined value
      > > int i2 = int(); // i2 now holds 0
      > > }
      > >
      > > Is the above true?[/color][/color]

      For more on this read an ongoing thread on c.l.c++.moderat ed "int(), float(),
      bool()".


      Comment

      • Ivan Vecerina

        #4
        Re: Build-in types initialization

        "Marcin Kalicinski" <kalita@poczta. onet.pl> wrote in message
        news:c1i7fp$dal $1@korweta.task .gda.pl...
        | void f()
        | {
        | int i1; // i1 now holds undefined value
        | int i2 = int(); // i2 now holds 0
        | }
        |
        | Is the above true?

        Yes.
        This is useful to know and use especially in generic code:
        T val = T(); // ensures that built-ins also get initialized

        Consider also default-parameter values:
        vector(size_typ e n, const T& v = T(), const A& al = A());


        Regards,
        Ivan
        --
        http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


        Comment

        • Marcin Kalicinski

          #5
          Re: Build-in types initialization

          Hi all,

          Thanks for your answer, but if it is true, how do I define my custom class
          so that it follows the same schema? It seems impossible, because there's no
          way to distinguish between these 2 types of initialization when writing
          class definition. Both are handled by default constructor.

          To be specific, I'd like to define class Vector3, which is a 3 dimensional
          vector. If I initialize coordinates to zeroes in default constructor, I get
          behavior 'i2'. If I do not initialize, I get behavior 'i1'. But I'd like to
          get both if them, so that Vector3 does not differ in this important point
          from built-in types. Additionally, Vector3 is to be used in
          performance-critical code, so I'd like to avoid unnecesary initialization if
          possible - so 'i1' behavior is probably a must to have.

          Best regards,
          Marcin

          U¿ytkownik "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > napisa³ w
          wiadomoœci news:c1i8bg$1ii hqh$1@ID-221354.news.uni-berlin.de...

          "Marcin Kalicinski" <kalita@poczta. onet.pl> wrote in message
          news:c1i7fp$dal $1@korweta.task .gda.pl...[color=blue]
          > Hi,
          >
          > void f()
          > {
          > int i1; // i1 now holds undefined value
          > int i2 = int(); // i2 now holds 0
          > }
          >
          > Is the above true?[/color]

          Yes



          Comment

          • Sharad Kala

            #6
            Re: Build-in types initialization


            "Marcin Kalicinski" <kalita@poczta. onet.pl> wrote in message
            news:c1ibcm$e3b $1@korweta.task .gda.pl...[color=blue]
            > Hi all,
            >
            > Thanks for your answer, but if it is true, how do I define my custom class
            > so that it follows the same schema? It seems impossible, because there's no
            > way to distinguish between these 2 types of initialization when writing
            > class definition. Both are handled by default constructor.
            >
            > To be specific, I'd like to define class Vector3, which is a 3 dimensional
            > vector. If I initialize coordinates to zeroes in default constructor, I get
            > behavior 'i2'. If I do not initialize, I get behavior 'i1'. But I'd like to
            > get both if them, so that Vector3 does not differ in this important point
            > from built-in types. Additionally, Vector3 is to be used in
            > performance-critical code, so I'd like to avoid unnecesary initialization if
            > possible - so 'i1' behavior is probably a must to have.[/color]

            You got it wrong.
            The default constructor for int, float, bool initializes them to 0 /false.
            But that does not mean that ints, floats etc in UDTs will also be default
            initialized in the default constructor.

            In this code -

            #include <iostream>
            using namespace std;
            struct A{
            int i;
            A(){}
            A(int I):i(I){}
            };

            int main(){

            A a1 = A(); // Default const
            cout << a1.i; // Print garbage..don't expect 0
            A a2(10);
            cout << a2.i; // Print 10
            }


            Comment

            • Andrey Tarasevich

              #7
              Re: Build-in types initialization

              Sharad Kala wrote:[color=blue]
              > ...
              > The default constructor for int, float, bool initializes them to 0 /false.
              > But that does not mean that ints, floats etc in UDTs will also be default
              > initialized in the default constructor.
              > ...[/color]

              There are different ways to formalize this behavior of scalar (and POD)
              types in C++, but the standard way is to assume that scalar types have
              no constructors at all, which is what C++ standard says.

              --
              Best regards,
              Andrey Tarasevich

              Comment

              • Marcin Kalicinski

                #8
                Re: Build-in types initialization

                Hi,

                A a1 = A(); // Default const
                cout << a1.i; // Print garbage..don't expect 0

                The above is true. But if we replace A with a built-in type, say int, its
                value will be initialized by 1st line. I'm worried, because this makes
                user-defined types inferior in some way to built-in types. At least it makes
                them behave differently, as there's no way to distinguish between these 2
                construction types (available with built-in types):

                1. Construct and leave members uninitialized
                2. Construct and initialize members to default values, specific for each
                member

                Or at least there's no elegant way (by elegant I mean the way built-in types
                are initialized).

                Marcin


                U¿ytkownik "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > napisa³ w
                wiadomoœci news:c1id0c$1i5 nop$1@ID-221354.news.uni-berlin.de...

                "Marcin Kalicinski" <kalita@poczta. onet.pl> wrote in message
                news:c1ibcm$e3b $1@korweta.task .gda.pl...[color=blue]
                > Hi all,
                >
                > Thanks for your answer, but if it is true, how do I define my custom class
                > so that it follows the same schema? It seems impossible, because there's[/color]
                no[color=blue]
                > way to distinguish between these 2 types of initialization when writing
                > class definition. Both are handled by default constructor.
                >
                > To be specific, I'd like to define class Vector3, which is a 3 dimensional
                > vector. If I initialize coordinates to zeroes in default constructor, I[/color]
                get[color=blue]
                > behavior 'i2'. If I do not initialize, I get behavior 'i1'. But I'd like[/color]
                to[color=blue]
                > get both if them, so that Vector3 does not differ in this important point
                > from built-in types. Additionally, Vector3 is to be used in
                > performance-critical code, so I'd like to avoid unnecesary initialization[/color]
                if[color=blue]
                > possible - so 'i1' behavior is probably a must to have.[/color]

                You got it wrong.
                The default constructor for int, float, bool initializes them to 0 /false.
                But that does not mean that ints, floats etc in UDTs will also be default
                initialized in the default constructor.

                In this code -

                #include <iostream>
                using namespace std;
                struct A{
                int i;
                A(){}
                A(int I):i(I){}
                };

                int main(){

                A a1 = A(); // Default const
                cout << a1.i; // Print garbage..don't expect 0
                A a2(10);
                cout << a2.i; // Print 10
                }



                Comment

                • Sharad Kala

                  #9
                  Re: Build-in types initialization


                  "Marcin Kalicinski" <kalita@poczta. onet.pl> wrote in message
                  news:c1kaqj$c01 $1@korweta.task .gda.pl...[color=blue]
                  > Hi,[/color]
                  Hello,
                  Please don't top-post.
                  [color=blue]
                  > A a1 = A(); // Default const
                  > cout << a1.i; // Print garbage..don't expect 0
                  >
                  > The above is true. But if we replace A with a built-in type, say int, its
                  > value will be initialized by 1st line. I'm worried, because this makes
                  > user-defined types inferior in some way to built-in types. At least it makes
                  > them behave differently, as there's no way to distinguish between these 2
                  > construction types (available with built-in types):[/color]

                  Different, but I don't think that it makes them inferior.
                  [color=blue]
                  > 1. Construct and leave members uninitialized[/color]

                  This is your particular need. People usually want to initialize the members of
                  their class.
                  Furthermore intialization lists are there so that this happens efficiently.
                  [color=blue]
                  > 2. Construct and initialize members to default values, specific for each
                  > member
                  > Or at least there's no elegant way (by elegant I mean the way built-in types
                  > are initialized).[/color]

                  I can think of this solution, probably have a bool value in your constructor
                  something like -

                  #include <iostream>
                  using namespace std;
                  struct A{
                  int i;
                  A(bool init=false):i(( init==true)?int ():i){}
                  };

                  int main(){
                  A a1 = A(); // Default const
                  cout << a1.i; // Print garbage
                  A a2(true); // Iniatialize with defaults
                  cout << a2.i; // Print 0
                  }


                  Comment

                  Working...