Constructor Question

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

    Constructor Question

    I need to have a constructor that is passed a integer number that defines
    the size of an array in the private area of the Class. Is that possible?

    For example in main() the user is asked for the number of lanes. Then when
    the object is declared after that it is passed that integer, which then
    creates an array of pointers.


  • Gianni Mariani

    #2
    Re: Constructor Question

    - Steve - wrote:[color=blue]
    > I need to have a constructor that is passed a integer number that defines
    > the size of an array in the private area of the Class. Is that possible?
    >
    > For example in main() the user is asked for the number of lanes. Then when
    > the object is declared after that it is passed that integer, which then
    > creates an array of pointers.
    >
    >[/color]

    you could use a std::vector


    i.e.

    class Lanes
    {

    std::vector< Lane * > m_lane_array;

    public:
    Lanes( int size )
    : m_lane_array( size )
    {
    }
    };


    Comment

    • TR

      #3
      Re: Constructor Question

      "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
      news:k8pPa.4548 $R92.2613@news2 .central.cox.ne t...[color=blue]
      > I need to have a constructor that is passed a integer number that defines
      > the size of an array in the private area of the Class. Is that possible?[/color]

      Yes, constructors can take parameters like normal functions. Use an
      std::vector in your private area and call .resize() in your constructor to
      set the array size. In fact you don't even need to resize it, as it'll grow
      as data is added. Get hold of an elementary C++ reference to learn more.


      Comment

      • dwrayment

        #4
        Re: Constructor Question

        vectors are fine if you like std, personally i use std sparingly

        all ya have to do is use new

        Object *_array = new Objects[t];

        or

        typedef Object *PObject;
        Object **_array = new PObject[t];


        depending on what exactly your looking to do




        "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
        news:k8pPa.4548 $R92.2613@news2 .central.cox.ne t...[color=blue]
        > I need to have a constructor that is passed a integer number that defines
        > the size of an array in the private area of the Class. Is that possible?
        >
        > For example in main() the user is asked for the number of lanes. Then[/color]
        when[color=blue]
        > the object is declared after that it is passed that integer, which then
        > creates an array of pointers.
        >
        >[/color]


        Comment

        • Josephine Schafer

          #5
          Re: Constructor Question


          "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
          news:k8pPa.4548 $R92.2613@news2 .central.cox.ne t...[color=blue]
          > I need to have a constructor that is passed a integer number that defines
          > the size of an array in the private area of the Class. Is that possible?
          >
          > For example in main() the user is asked for the number of lanes. Then[/color]
          when[color=blue]
          > the object is declared after that it is passed that integer, which then
          > creates an array of pointers.
          >[/color]

          You should declare your constructor to be explicit because the parameter
          passed to the
          constructor determines the object's configuration. This way you prevent any
          implicit
          conversions that may otherwise occur inadvertently.

          With best wishes,
          J.Schafer


          Comment

          • John Harrison

            #6
            Re: Constructor Question


            "TR" <no@spam.com> wrote in message
            news:R_pPa.2937 $797.1325@news-binary.blueyond er.co.uk...[color=blue]
            > "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
            > news:k8pPa.4548 $R92.2613@news2 .central.cox.ne t...[color=green]
            > > I need to have a constructor that is passed a integer number that[/color][/color]
            defines[color=blue][color=green]
            > > the size of an array in the private area of the Class. Is that[/color][/color]
            possible?[color=blue]
            >
            > Yes, constructors can take parameters like normal functions. Use an
            > std::vector in your private area and call .resize() in your constructor to
            > set the array size. In fact you don't even need to resize it, as it'll[/color]
            grow[color=blue]
            > as data is added. Get hold of an elementary C++ reference to learn more.
            >[/color]

            Err, you can't resize an array. I think you are thinking of a vector.

            john


            Comment

            • John Harrison

              #7
              Re: Constructor Question


              "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
              news:k8pPa.4548 $R92.2613@news2 .central.cox.ne t...[color=blue]
              > I need to have a constructor that is passed a integer number that defines
              > the size of an array in the private area of the Class. Is that possible?
              >
              > For example in main() the user is asked for the number of lanes. Then[/color]
              when[color=blue]
              > the object is declared after that it is passed that integer, which then
              > creates an array of pointers.
              >[/color]

              Don't use an array, use a vector. Something like this

              #include <vector>

              class LaneVector
              {
              public:
              LaneVector(int num_lanes) : lanes(num_lanes )
              {
              }
              private:
              std::vector<Lan e> lanes;
              };

              You should really learn about vectors, make your life much easier.

              john


              Comment

              • Karl Heinz Buchegger

                #8
                Re: Constructor Question



                dwrayment wrote:[color=blue]
                >
                > vectors are fine if you like std, personally i use std sparingly
                >
                > all ya have to do is use new
                >
                > Object *_array = new Objects[t];
                >[/color]

                .... and by thus have opened the 'Hey why use something
                simple when there is a more complicate way' season.

                By doing the memory management by yourself, your class
                suddenly also needs: a destructor, an assignment operator,
                a copy constructor. And all of them have, of course, to be
                written correctly!

                So why would you want to do this, when vector is available
                and saves you from all this possible pitfalls?

                --
                Karl Heinz Buchegger
                kbuchegg@gascad .at

                Comment

                • Peter Gregory

                  #9
                  Re: Constructor Question

                  John Harrison wrote:
                  [color=blue]
                  >
                  > "TR" <no@spam.com> wrote in message
                  > news:R_pPa.2937 $797.1325@news-binary.blueyond er.co.uk...[color=green]
                  >> "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
                  >> news:k8pPa.4548 $R92.2613@news2 .central.cox.ne t...[color=darkred]
                  >> > I need to have a constructor that is passed a integer number that[/color][/color]
                  > defines[color=green][color=darkred]
                  >> > the size of an array in the private area of the Class. Is that[/color][/color]
                  > possible?[color=green]
                  >>
                  >> Yes, constructors can take parameters like normal functions. Use an
                  >> std::vector in your private area and call .resize() in your constructor
                  >> to set the array size. In fact you don't even need to resize it, as it'll[/color]
                  > grow[color=green]
                  >> as data is added. Get hold of an elementary C++ reference to learn more.
                  >>[/color]
                  >
                  > Err, you can't resize an array. I think you are thinking of a vector.
                  >
                  > john[/color]

                  That's why s/he said quite clearly "Use an STD::vector..." .

                  pete

                  Comment

                  • John Harrison

                    #10
                    Re: Constructor Question


                    "Peter Gregory" <peter.gregory@ durham.ac.uk> wrote in message
                    news:bemjql$edq $1@sirius.dur.a c.uk...[color=blue]
                    > John Harrison wrote:
                    >[color=green]
                    > >
                    > > "TR" <no@spam.com> wrote in message
                    > > news:R_pPa.2937 $797.1325@news-binary.blueyond er.co.uk...[color=darkred]
                    > >> "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
                    > >> news:k8pPa.4548 $R92.2613@news2 .central.cox.ne t...
                    > >> > I need to have a constructor that is passed a integer number that[/color]
                    > > defines[color=darkred]
                    > >> > the size of an array in the private area of the Class. Is that[/color]
                    > > possible?[color=darkred]
                    > >>
                    > >> Yes, constructors can take parameters like normal functions. Use an
                    > >> std::vector in your private area and call .resize() in your constructor
                    > >> to set the array size. In fact you don't even need to resize it, as[/color][/color][/color]
                    it'll[color=blue][color=green]
                    > > grow[color=darkred]
                    > >> as data is added. Get hold of an elementary C++ reference to learn[/color][/color][/color]
                    more.[color=blue][color=green][color=darkred]
                    > >>[/color]
                    > >
                    > > Err, you can't resize an array. I think you are thinking of a vector.
                    > >
                    > > john[/color]
                    >
                    > That's why s/he said quite clearly "Use an STD::vector..." .
                    >
                    > pete
                    >[/color]

                    Hmm, shortly followed by "call .resize() in your constructor to set the
                    array size", but maybe I should learn to read.

                    Also the statement "In fact you don't even need to resize it, as it'll grow
                    as data is added." is misleading at best.

                    john


                    Comment

                    • dwrayment

                      #11
                      Re: Constructor Question


                      if you think thats complicated im sorry for you. to me its
                      not that complicated to add copy costructors and a destructor.
                      if fact its simpler and less bloated than having to include std.
                      but thats just me.

                      it gives me piece of mind knowing that im not using some other persons code.
                      ive even made my own classes for strings and several other data types, for
                      the same reason and because to otfen std does not have a function that id
                      like, or has functions i could care less about.

                      to me std is a lazy mans way to program. you wont become a better
                      programmer that way. while there are some parts of std that i do find
                      useful <algotyhms.h> for example. i tend to stay away from using the
                      containers because they post no advantage to me. i can write a more
                      efficient, more effective class than std with a little bit of effort.

                      To each his own. Im not gonna ask you why you choose to use stl thats your
                      choice, but mine however is not (most of the time).



                      "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
                      news:3F0E81BF.A 23F03BC@gascad. at...[color=blue]
                      >
                      >
                      > dwrayment wrote:[color=green]
                      > >
                      > > vectors are fine if you like std, personally i use std sparingly
                      > >
                      > > all ya have to do is use new
                      > >
                      > > Object *_array = new Objects[t];
                      > >[/color]
                      >
                      > ... and by thus have opened the 'Hey why use something
                      > simple when there is a more complicate way' season.
                      >
                      > By doing the memory management by yourself, your class
                      > suddenly also needs: a destructor, an assignment operator,
                      > a copy constructor. And all of them have, of course, to be
                      > written correctly!
                      >
                      > So why would you want to do this, when vector is available
                      > and saves you from all this possible pitfalls?
                      >
                      > --
                      > Karl Heinz Buchegger
                      > kbuchegg@gascad .at[/color]


                      Comment

                      • John Harrison

                        #12
                        Re: Constructor Question


                        "dwrayment" <dwrayment@roge rs.com> wrote in message
                        news:HGCPa.4610 $1aB1.2879@news 02.bloor.is.net .cable.rogers.c om...[color=blue]
                        >
                        > if you think thats complicated im sorry for you. to me its
                        > not that complicated to add copy costructors and a destructor.
                        > if fact its simpler and less bloated than having to include std.
                        > but thats just me.
                        >[/color]

                        It's amazing how difficult it can be to write exception safe code. Its not a
                        skill I would think more than 1% of programmers have (I think that is being
                        generous). Maybe you are in that 1%, or maybe you don't care about exception
                        safety.

                        john


                        Comment

                        • TR

                          #13
                          Re: Constructor Question

                          "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                          news:bemk2d$6q2 8k$1@ID-196037.news.uni-berlin.de...[color=blue]
                          > Hmm, shortly followed by "call .resize() in your constructor to set the
                          > array size", but maybe I should learn to read.
                          >
                          > Also the statement "In fact you don't even need to resize it, as it'll[/color]
                          grow[color=blue]
                          > as data is added." is misleading at best.[/color]

                          Stop it.


                          Comment

                          • John Harrison

                            #14
                            Re: Constructor Question


                            "TR" <no@spam.com> wrote in message
                            news:_EEPa.851$ uO5.739@news-binary.blueyond er.co.uk...[color=blue]
                            > "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                            > news:bemk2d$6q2 8k$1@ID-196037.news.uni-berlin.de...[color=green]
                            > > Hmm, shortly followed by "call .resize() in your constructor to set the
                            > > array size", but maybe I should learn to read.
                            > >
                            > > Also the statement "In fact you don't even need to resize it, as it'll[/color]
                            > grow[color=green]
                            > > as data is added." is misleading at best.[/color]
                            >
                            > Stop it.
                            >[/color]

                            Being petty you mean? OK, apologies.

                            john


                            Comment

                            • Karl Heinz Buchegger

                              #15
                              Re: Constructor Question



                              dwrayment wrote:[color=blue]
                              >
                              > if you think thats complicated im sorry for you.[/color]

                              ????

                              I quote you:
                              [color=blue][color=green][color=darkred]
                              > > > vectors are fine if you like std, personally i use std sparingly
                              > > >
                              > > > all ya have to do is use new
                              > > >
                              > > > Object *_array = new Objects[t];
                              > > >[/color][/color][/color]

                              That's completely misleading. Especially for newbies.
                              It is not true, that 'all you have to do is ...'
                              You also have to do, and you also have to do ...
                              [color=blue]
                              > to me its
                              > not that complicated to add copy costructors and a destructor.[/color]

                              Fine for you.
                              [color=blue]
                              > if fact its simpler and less bloated than having to include std.
                              > but thats just me.[/color]

                              You forgot about the assignment operator :-)
                              Also: Can you write a completely correct copy constructor and/or
                              assignment operator which can also handle exceptions correctly?

                              It's not so complicated, but I have to admit I need a few minutes
                              to write them, and I have written lots of them in the past.
                              [color=blue]
                              >
                              > it gives me piece of mind knowing that im not using some other persons code.[/color]

                              So you don't use printf or the stream objects and instead write one
                              on your own. Oh, and of course you don't use string objects. I wonder
                              if you buy pens in a shop or do you make your own ink and use goose feathers.


                              If you want to do memory management on your own, this is your thing.
                              I won't comment on that. But if you start recommending this to newbies,
                              I will comment on that.

                              --
                              Karl Heinz Buchegger
                              kbuchegg@gascad .at

                              Comment

                              Working...