why it's not possible calling constructor from constructor?

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

    why it's not possible calling constructor from constructor?

    why definition of two constructors like these is not possible in c++???

    -----------------------
    date::date(cons t int d, const int m, const int y, const int ora, const int
    mi, const int se){
    day_ = d;
    month_ = m;
    year_ = y;

    secondo_ = se;
    minuto_ = mi;
    ora_ = ora;
    }

    date::date(){ date(0,0,0,0,0, 0); }
    ---------------

    thanx
    Giulio




  • Simon Saunders

    #2
    Re: why it's not possible calling constructor from constructor?

    "Giulio" <giulio.gL E V A@email.it> wrote in message
    news:c8eKa.3929 3$Fr5.850871@to rnado.fastwebne t.it...[color=blue]
    > why definition of two constructors like these is not possible in c++???
    >
    > -----------------------
    > date::date(cons t int d, const int m, const int y, const int ora, const int
    > mi, const int se){
    > day_ = d;
    > month_ = m;
    > year_ = y;
    >
    > secondo_ = se;
    > minuto_ = mi;
    > ora_ = ora;
    > }
    >
    > date::date(){ date(0,0,0,0,0, 0); }
    > ---------------
    >[/color]

    The second constructor creates an unnamed temporary object; that's just the
    way C++ works. The usual workaround is to write a private member function to
    do the initialization and call that from both constructors, for example:

    date::date(int d, int m, int y, int ora, int mi, int se)
    {
    init(d, m, y, ora, mi, se);
    }

    date::date()
    {
    init(0, 0, 0, 0, 0, 0);
    }

    void date::init(int d, int m, int y, int ora, int mi, int se)
    {
    day_ = d;
    month_ = m;
    year_ = y;
    secondo_ = se;
    minuto_ = mi;
    ora_ = ora;
    }


    Comment

    • Patrick Kowalzick

      #3
      Re: why it's not possible calling constructor from constructor?

      > class date {[color=blue]
      > public:
      > date(const int & d=0,const int & m=0, const int & y=0, const int & ora=0,
      > const int & mi=0, const int & se=0)
      > : day(d), month(m),year(y ),secondo(se),m inuto(mi),ora(o ra) {}
      > private:
      > int day,month,year, secondo,minuto, ora;
      > };[/color]
      Argh, forgotten something

      with this constructor you have a lot constructors - perhaps too much. You
      have to check this in each case:

      date();
      date(int);
      date(int,int);
      date(int,int,in t);
      date(int,int,in t,int);
      date(int,int,in t,int,int);
      date(int,int,in t,int,int,int);

      Greetings,
      Patrick


      Comment

      • tom_usenet

        #4
        Re: why it's not possible calling constructor from constructor?

        On Wed, 25 Jun 2003 11:42:20 +0200, "Giulio" <giulio.gL E V
        A@email.it> wrote:
        [color=blue]
        >why definition of two constructors like these is not possible in c++???[/color]

        The construction model in C++ is quite strict (mostly due to the fact
        that C++ has "value-based" UDTs rather than just pointers, as in
        Java). Basically, by the time you enter the body of your constructor,
        all of your base classes and all of your member variables will already
        have been constructed. So there is no way to invoke another
        constructor from the body of your constructor, because that would
        involve double construction of all your variables.

        If you just want to share just the body of the constructor, then
        refactor it out into a separate function that both of your
        constructors call.
        [color=blue]
        >
        >-----------------------
        >date::date(con st int d, const int m, const int y, const int ora, const int
        >mi, const int se){
        > day_ = d;
        > month_ = m;
        > year_ = y;
        >
        > secondo_ = se;
        > minuto_ = mi;
        > ora_ = ora;
        >}[/color]

        Normally that would be done using an initializer list:
        date::date(cons t int d, const int m, const int y, const int ora, const
        int mi, const int se)
        :day_(d),
        month_(m),
        year_(y),
        secondo_(se),
        minuto_(mi),
        ora_(ora)
        {
        }

        [color=blue]
        >date::date() { date(0,0,0,0,0, 0); }[/color]

        date::date()
        :day_(0),
        month_(0),
        year_(0),
        secondo_(0),
        minuto_(0),
        ora_(0)
        {
        }

        As you can see, there is no code in the body at all, so there's no
        common code to share anyway.

        If you want to do it your way, do it like this:

        void date::initialis e(const int d, const int m, const int y, const int
        ora, const int mi, const int se)
        {
        day_ = d;
        month_ = m;
        year_ = y;

        secondo_ = se;
        minuto_ = mi;
        ora_ = ora;
        //any checking?
        }

        date::date()
        {
        initialise(0,0, 0,0,0,0);
        }

        date::date(cons t int d, const int m, const int y, const int ora, const
        int mi, const int se)
        {
        initialise(d, m, y, ora, mi, se);
        }

        but for the level of code sharing involved, it doesn't seem worth it.

        Tom

        Comment

        • Karl Heinz Buchegger

          #5
          Re: why it's not possible calling constructor from constructor?



          Patrick Kowalzick wrote:[color=blue]
          >
          >
          > An answer to your question. Every call of a constructor instanciates an
          > object. So calling the constructor twice should create two objects. While
          > this makes no sense it is forbidden to call a constructor from a
          > constructor. (95% sure ;-) )[/color]

          Strictly speaking this is not correct. In fact you can't call
          a constructor, since a constructor is a function with no name.

          It is the other way round: Whenever you create an object, a
          constructor is called.
          Therefore the call of the constructor is a side effect of createing
          an object, not the other way round.

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • Victor Bazarov

            #6
            Re: why it's not possible calling constructor from constructor?

            "Chandra Shekhar Kumar" <chandra.kumar@ oracle.com> wrote...[color=blue]
            >
            > it is possible in C++.[/color]

            Would you stop making those bogus statements? I can understand
            how it may be amusing to you, but it is definitely confusing to
            those who expect a correct answer.

            It is impossible to _call_ a contstructor. There is no way in
            the language to "forward" the construction of the object from one
            constructor to another like in Java. If there is some common
            code to be executed by both constructors, the best place to put
            it is a separate function which will be called by the constructors.

            As to the OP's problem, the simplest thing to do is to have one
            constructor with all arguments (d,m,y,ora,mi,s e) with default
            argument values set to 0:

            date::date(int d = 0, int m = 0, int y = 0,
            int ora = 0, int mi = 0, int se = 0) :
            day_(d), month_(m), year_(y),
            ora_(ora), minuto_(mi), secondo_(se)
            {
            }
            [color=blue]
            > the only thing imp here is what do u want to do with this....
            > i think u want to keep the date(const int d, const int m, const int y,[/color]
            const[color=blue]
            > int ora, ...) as private so the client can't use it directly...and then u[/color]
            want[color=blue]
            > to have a public date() which calls the above private stuff with some[/color]
            specific[color=blue]
            > values.....
            >[color=green]
            > > why definition of two constructors like these is not possible in c++???
            > >
            > > -----------------------
            > > date::date(cons t int d, const int m, const int y, const int ora, const[/color][/color]
            int[color=blue][color=green]
            > > mi, const int se){
            > > day_ = d;
            > > month_ = m;
            > > year_ = y;
            > >
            > > secondo_ = se;
            > > minuto_ = mi;
            > > ora_ = ora;
            > > }
            > >
            > > date::date(){ date(0,0,0,0,0, 0); }
            > >[/color]
            >[/color]


            Comment

            • Ron Natalie

              #7
              Re: why it's not possible calling constructor from constructor?


              "Chandra Shekhar Kumar" <chandra.kumar@ oracle.com> wrote in message news:3EF8D82F.6 42702AC@oracle. com...[color=blue]
              >
              > it is possible in C++.[/color]

              It is NOT possible in C++.
              [color=blue]
              > i think u want to keep the date(const int d, const int m, const int y, const
              > int ora, ...) as private so the client can't use it directly...and then u want
              > to have a public date() which calls the above private stuff with some specific
              > values.....[/color]

              What makes you think he wants to do that.
              [color=blue][color=green]
              > > date::date(){ date(0,0,0,0,0, 0); }
              > >[/color]
              >[/color]
              This does not call the constructor. You can NOT call constructors at all.
              The syntax above that appears to be a call is the creation of a temporary date
              object in a simple expression that ceases to be at the end of the statement.
              It insidiously compiles, but does nothing.


              Comment

              • Giulio

                #8
                Re: why it's not possible calling constructor from constructor?

                thanx to all for the exhaustive answers, in the future I'll use separate
                functions used as constructors -init(...)- in this case because there are
                only two constructors for class date I'll leave them separate.

                this is a great NG
                Giulio


                Comment

                • tom_usenet

                  #9
                  Re: why it's not possible calling constructor from constructor?

                  On Wed, 25 Jun 2003 11:42:20 +0200, "Giulio" <giulio.gL E V
                  A@email.it> wrote:
                  [color=blue]
                  >why definition of two constructors like these is not possible in c++???
                  >
                  >-----------------------
                  >date::date(con st int d, const int m, const int y, const int ora, const int
                  >mi, const int se){
                  > day_ = d;
                  > month_ = m;
                  > year_ = y;
                  >
                  > secondo_ = se;
                  > minuto_ = mi;
                  > ora_ = ora;
                  >}
                  >
                  >date::date() { date(0,0,0,0,0, 0); }[/color]

                  Ahh, I forgot the super hacky, completely illegal solution:

                  date::date(){
                  new (this) date(0,0,0,0,0, 0);
                  }

                  It will probably work though! ;)

                  Tom

                  Comment

                  • Patrick Kowalzick

                    #10
                    Re: why it's not possible calling constructor from constructor?

                    > >. (95% sure ;-) )[color=blue]
                    >
                    > Strictly speaking this is not correct.[/color]

                    These are the famous outliers. I wanted to write 99% first. PUH ;-)

                    Thanks
                    Patrick


                    Comment

                    Working...