Inititialing list/vector inside a constructor

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

    Inititialing list/vector inside a constructor

    Hello Group,

    I have a class

    class Simple2 {
    publica:
    Simple2();
    list<int> *l1;
    list<int> l2;
    };

    I am writing the constructor

    Simple2::Simple 2 {

    // It is easy to initialize l1

    l1 = new list<int>;

    }

    How do I initialize l2 data member? The same thing I would like to ask
    for vector class, if
    it is an object and not a pointer how do you initialize template
    classes.

    Thanks for your help.

    Nagrik

  • Victor Bazarov

    #2
    Re: Inititialing list/vector inside a constructor

    arun wrote:[color=blue]
    > I have a class
    >
    > class Simple2 {
    > publica:
    > Simple2();
    > list<int> *l1;
    > list<int> l2;
    > };
    >
    > I am writing the constructor
    >
    > Simple2::Simple 2 {[/color]

    Simple2::Simple 2() {
    [color=blue]
    >
    > // It is easy to initialize l1
    >
    > l1 = new list<int>;[/color]

    That's not initialisation. It's assignment. Initialisation would be
    in the initialiser list. Yours is empty.
    [color=blue]
    >
    > }
    >
    > How do I initialize l2 data member?[/color]

    Why do you think you need to? It's default-initialised for you.
    [color=blue]
    > The same thing I would like to
    > ask for vector class, if
    > it is an object and not a pointer how do you initialize template
    > classes.[/color]

    Put them in the constructor initialiser list. What book on C++ are you
    reading that doesn't explain this extremely basic feature of the language?

    Simple2::Simple 2() : l1(new list<int>), l2()
    {
    }

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


    Comment

    • Rolf Magnus

      #3
      Re: Inititialing list/vector inside a constructor

      arun wrote:
      [color=blue]
      > Hello Group,
      >
      > I have a class
      >
      > class Simple2 {
      > publica:[/color]

      public:
      [color=blue]
      > Simple2();
      > list<int> *l1;
      > list<int> l2;
      > };
      >
      > I am writing the constructor
      >
      > Simple2::Simple 2 {
      >
      > // It is easy to initialize l1[/color]

      Then do so. Below, you're not initializing. You are assigning.
      [color=blue]
      > l1 = new list<int>;
      >
      > }
      >
      > How do I initialize l2 data member?[/color]

      With what? If the answer is "nothing", you're done, since it is
      automatically defaul-initialized. That's the glory of classes.
      [color=blue]
      > The same thing I would like to ask for vector class, if
      > it is an object and not a pointer how do you initialize template
      > classes.[/color]

      It has nothing to do with it being a template instance. You initialize it
      like any other class.

      Comment

      • Earl Purple

        #4
        Re: Inititialing list/vector inside a constructor


        Victor Bazarov wrote:
        [color=blue]
        > arun wrote:[color=green]
        > > The same thing I would like to
        > > ask for vector class, if
        > > it is an object and not a pointer how do you initialize template
        > > classes.[/color]
        >
        > Put them in the constructor initialiser list. What book on C++ are you
        > reading that doesn't explain this extremely basic feature of the language?
        >
        > Simple2::Simple 2() : l1(new list<int>), l2()
        > {
        > }[/color]

        but of course you cannot initialise them with data in a way you would
        expect thus:

        class HasVector
        {
        vector <int > v;

        public:

        HasVector() : v( { 1, 2, 3, 4, 5 } ) // illegal
        {
        }
        };

        The workaround is to define the initialisation array in anonymous
        namespace.

        namespace {
        const int vecInit[] = { 1, 2, 3, 4, 5 };
        const size_t vecInitSize = sizeof( vecInit ) / sizeof( vecInit[0] );
        }

        HasVector::HasV ector()
        : v( &vecInit[0], &vecInit[vecInitSize] )
        {
        }

        Comment

        • Victor Bazarov

          #5
          Re: Inititialing list/vector inside a constructor

          Earl Purple wrote:[color=blue]
          > Victor Bazarov wrote:
          >[color=green]
          >> arun wrote:[color=darkred]
          >>> The same thing I would like to
          >>> ask for vector class, if
          >>> it is an object and not a pointer how do you initialize template
          >>> classes.[/color]
          >>
          >> Put them in the constructor initialiser list. What book on C++ are
          >> you reading that doesn't explain this extremely basic feature of the
          >> language?
          >>
          >> Simple2::Simple 2() : l1(new list<int>), l2()
          >> {
          >> }[/color]
          >
          > but of course you cannot initialise them with data in a way you would
          > expect thus:
          >
          > class HasVector
          > {
          > vector <int > v;
          >
          > public:
          >
          > HasVector() : v( { 1, 2, 3, 4, 5 } ) // illegal
          > {
          > }
          > };
          >
          > The workaround is to define the initialisation array in anonymous
          > namespace.
          >
          > namespace {
          > const int vecInit[] = { 1, 2, 3, 4, 5 };
          > const size_t vecInitSize = sizeof( vecInit ) / sizeof( vecInit[0] );
          > }
          >
          > HasVector::HasV ector()
          > : v( &vecInit[0], &vecInit[vecInitSize] )[/color]

          I think it was just recently discussed that '&array[itssize]' has UB.
          What's wrong with writing simply

          v(vecInit, vecInit + vecInitSize)

          ?
          [color=blue]
          > {
          > }[/color]

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • mlimber

            #6
            Re: Inititialing list/vector inside a constructor

            Earl Purple wrote:[color=blue]
            > Victor Bazarov wrote:[color=green]
            > > arun wrote:[color=darkred]
            > > > The same thing I would like to
            > > > ask for vector class, if
            > > > it is an object and not a pointer how do you initialize template
            > > > classes.[/color]
            > >
            > > Put them in the constructor initialiser list. What book on C++ are you
            > > reading that doesn't explain this extremely basic feature of the language?
            > >
            > > Simple2::Simple 2() : l1(new list<int>), l2()
            > > {
            > > }[/color]
            >
            > but of course you cannot initialise them with data in a way you would
            > expect thus:
            >
            > class HasVector
            > {
            > vector <int > v;
            >
            > public:
            >
            > HasVector() : v( { 1, 2, 3, 4, 5 } ) // illegal
            > {
            > }
            > };
            >
            > The workaround is to define the initialisation array in anonymous
            > namespace.
            >
            > namespace {
            > const int vecInit[] = { 1, 2, 3, 4, 5 };
            > const size_t vecInitSize = sizeof( vecInit ) / sizeof( vecInit[0] );
            > }
            >
            > HasVector::HasV ector()
            > : v( &vecInit[0], &vecInit[vecInitSize] )
            > {
            > }[/color]

            Assuming you fix the undefined behavior, that's not *the* work-around
            so much as *a* work-around. See this post for another option:



            Cheers! --M

            Comment

            • arun

              #7
              Re: Inititialing list/vector inside a constructor

              Hello Group,

              I asked this question because I read it, that if a class itself is a
              data member in another class then it must
              be instantiated explicitely. However, according to replies, which I
              got on this post it appears that if
              a class/template has a default constructor and it is a data member in
              another class, then that default
              constructor will be called regard less when you call the container
              class constructor and you don't have
              to do the following

              B has a data member C then you don't have to do this explicitely

              B() : C() //

              and following will work fine.

              B() {// code goes here }

              If this is the case then why does not the compiler initializes
              primitive data members like ints to 'zero'
              values. When I don't explicitely initialize ints then their values
              after the constructor is called is
              definitely not int but some thing else.

              Kindly explain this behaviour.

              thanks.

              arun

              Comment

              • Victor Bazarov

                #8
                Re: Inititialing list/vector inside a constructor

                arun wrote:[color=blue]
                > [...] it appears that if
                > a class/template has a default constructor and it is a data member in
                > another class, then that default
                > constructor will be called regard less when you call the container
                > class constructor and you don't have
                > to do the following
                >
                > B has a data member C then you don't have to do this explicitely
                >
                > B() : C() //
                >
                > and following will work fine.
                >
                > B() {// code goes here }
                >
                > If this is the case then why does not the compiler initializes
                > primitive data members like ints to 'zero'
                > values.[/color]

                Because the language Standard says so.
                [color=blue]
                > When I don't explicitely initialize ints then their values
                > after the constructor is called is
                > definitely not int but some thing else.[/color]

                Primitive data types are left uninitialised for optimization. If you
                don't care for a member of a primitive type to be initialised to
                anything, then the compiler is not the one to decide what to set it to.
                Classes that do have constructors must have their constructors invoked.
                That's just how class objects are created. Primitive types do not have
                constructors and therefore don't need to be initialised to anything.
                It's essentially the same behaviour that you'd get if you write

                int i;

                versus

                someclass obj;

                In the former case, 'i' is left uninitialised, whereas 'obj' is actually
                _constructed_ by invoking the [default] constructor from 'someclass'.

                V
                --
                Please remove capital 'A's when replying by e-mail
                I do not respond to top-posted replies, please don't ask


                Comment

                • Julián Albo

                  #9
                  Re: Inititialing list/vector inside a constructor

                  arun wrote:
                  [color=blue]
                  > If this is the case then why does not the compiler initializes
                  > primitive data members like ints to 'zero'
                  > values. When I don't explicitely initialize ints then their values
                  > after the constructor is called is
                  > definitely not int but some thing else.[/color]

                  For compatibility with C structs, and not use different rules depending if
                  the int is a member of a struct compatible with C or not. You can read "The
                  design and evolution of C++", for example, for the history and reasoning of
                  that type of things.

                  --
                  Salu2

                  Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php

                  Comment

                  Working...