Calling a constructor from another

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

    Calling a constructor from another

    How does a constructor of one class call another of the same class?
    When would this mechanism make sense or be required?


    PS: Two instances I saw are:

    (a) While using the Singleton pattern
    (b) While using the Factory pattern

  • Stephen Howe

    #2
    Re: Calling a constructor from another

    > How does a constructor of one class call another of the same class?

    It can't. C++ is not Java.

    Stephen Howe


    Comment

    • Jerry Coffin

      #3
      Re: Calling a constructor from another

      Sathyaish wrote:[color=blue]
      > How does a constructor of one class call another of the same class?[/color]

      About like anything else does -- and the result is the same as well --
      doing so constructs a new object.
      [color=blue]
      > When would this mechanism make sense or be required?[/color]

      Virtually never. The only time it makes sense is if constructing one
      object somehow involves first constructing another object of the same
      type. While I can see theoretical possibilities for this (e.g. creating
      a default-contructed object an then copying it to the current object to
      initialize most members) I've never actually seen a situation where
      this seemed like a good idea in real life.
      [color=blue]
      > PS: Two instances I saw are:
      >
      > (a) While using the Singleton pattern
      > (b) While using the Factory pattern[/color]

      I suspect what you're looking for is slightly different. If you have
      two ctors that have a consderable amount of common code, you do NOT
      normally want one ctor to call the other. Rather, you want to create a
      (usually private) function containing the common code, and then have
      both ctors call that common initialization function.

      As a final aside: officially you can't ever "call a constructor" -- you
      can only create an object, and the constructor gets invoked
      automatically in the process of constructing the object.

      --
      Later,
      Jerry.

      The universe is a figment of its own imagination.

      Comment

      • Alf P. Steinbach

        #4
        Re: Calling a constructor from another

        * Jerry Coffin:[color=blue]
        >
        > As a final aside: officially you can't ever "call a constructor" -- you
        > can only create an object, and the constructor gets invoked
        > automatically in the process of constructing the object.[/color]

        I've given up on some others, who after acknowledging the error of their
        ways in debate then immediately forget it, so that the same arguments and
        facts have had to be repeated later on, and then later on, and... It's
        religious or some sort of imagined group membership, I think. But for the
        record: the above is not "officially ". It's "unofficial ly", as in: "in the
        misleading terminology and internally inconsistent belief-set used by a
        small group of folks frequenting this newsgroup, who have a habit of
        immediately forgetting it when confronted with reason and the standard".

        It is of course valid to oversimplify in teaching, but then one should IMO
        be _very_ clear about this being an oversimplificat ion.

        And it is IMO simply wrong to use _misleading_ explanations, whether one
        explains that they're oversimplified or not.

        A less simplified explanation of the above: the language rules are designed
        so that, unless you use very low-level constructs designed to provide a
        loop-hole, you are guaranteed that every object of class type T receives
        exactly one T constructor call, before anything else happens to that object
        (which implies that you cannot ordinarily call a T constructor without
        creating a T object, or vice versa).

        Now even that is an oversimplificat ion, but not a misleading one: it's good
        enough as working model.

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Jerry Coffin

          #5
          Re: Calling a constructor from another

          Alf P. Steinbach wrote:[color=blue]
          > * Jerry Coffin:[color=green]
          > >
          > > As a final aside: officially you can't ever "call a constructor" --
          > > you can only create an object, and the constructor gets invoked
          > > automatically in the process of constructing the object.[/color]
          >
          > I've given up on some others, who after acknowledging the error of
          > their ways in debate then immediately forget it, so that the same
          > arguments and facts have had to be repeated later on, and then later
          > on, and... It's religious or some sort of imagined group membership,
          > I think. But for the record: the above is not "officially ". It's
          > "unofficial ly", as in: "in the misleading terminology and internally
          > inconsistent belief-set used by a small group of folks frequenting
          > this newsgroup, who have a habit of immediately forgetting it when
          > confronted with reason and the standard".[/color]

          You're right -- I started out by saying something on the order of
          "officially a ctor doesn't have a name, and you don't call it directly"
          but in trying to shorten that, I also made it less than accurate.

          While it's entirely possible that I'll make the same mistake again some
          day, I hope you'll take my assurance that if I do, it has more to do
          with being absent minded than anything having to do with religion or
          group membership -- and I hope you (or at least somebody) will correct
          me when/if I do.

          In any case, I hope I didn't mislead anybody too badly, and certainly
          apologize if I did.

          --
          Later,
          Jerry.

          The universe is a figment of its own imagination.

          Comment

          • Rolf Magnus

            #6
            Re: Calling a constructor from another

            Alf P. Steinbach wrote:
            [color=blue]
            > * Jerry Coffin:[color=green]
            >>
            >> As a final aside: officially you can't ever "call a constructor" -- you
            >> can only create an object, and the constructor gets invoked
            >> automatically in the process of constructing the object.[/color]
            >
            > I've given up on some others, who after acknowledging the error of their
            > ways in debate then immediately forget it, so that the same arguments and
            > facts have had to be repeated later on, and then later on, and...[/color]

            If I am one of those "others", then you should know that I never
            acknowledged what you call an "error". I simply disagree with you, and I
            still think you're the one who is wrong. However, I also gave up on trying
            to convince you of that.
            [color=blue]
            > It's religious or some sort of imagined group membership, I think.[/color]

            Bogus. It's just that more people have another view on this than you.
            [color=blue]
            > But for the record: the above is not "officially ". It's "unofficial ly",
            > as in: "in the misleading terminology and internally inconsistent
            > belief-set used by a small group of folks frequenting this newsgroup,
            > who have a habit of immediately forgetting it when confronted with
            > reason and the standard".[/color]

            Stop being so arrogant. We had those discussions, and you also were
            confronted with reason and the standard, and you also seemed to have
            forgotten those things. IMO, you are just interpreting the references to
            the standard that you gave in an incorrect way. I told you that, you
            ignored it. So don't point your finger at others.
            [color=blue]
            > It is of course valid to oversimplify in teaching, but then one should IMO
            > be _very_ clear about this being an oversimplificat ion.
            >
            > And it is IMO simply wrong to use _misleading_ explanations, whether one
            > explains that they're oversimplified or not.[/color]

            Yes, I agree on that. However, this is not one case of a misleading
            explanation.
            [color=blue]
            > A less simplified explanation of the above: the language rules are
            > designed so that, unless you use very low-level constructs designed to
            > provide a loop-hole, you are guaranteed that every object of class type T
            > receives exactly one T constructor call, before anything else happens to
            > that object (which implies that you cannot ordinarily call a T constructor
            > without creating a T object, or vice versa).[/color]

            Still, even if you use those loop-hole constructs, you must ensure that each
            object only receives one constructor call if you want your program to
            behave in a defined way.

            Comment

            • santosh

              #7
              Re: Calling a constructor from another

              Hello,
              I have two doubts on C++.
              1. float i=1.1;
              double d=1.1;
              if(i==d)
              printf("equal\n ");
              else
              printf("not-equal\n");
              what is the output?
              I am getting "not-equal" as output.
              Why it is so?


              Comment

              • Vyacheslav Kononenko

                #8
                Re: Calling a constructor from another


                santosh wrote:[color=blue]
                > Hello,
                > I have two doubts on C++.
                > 1. float i=1.1;
                > double d=1.1;
                > if(i==d)
                > printf("equal\n ");
                > else
                > printf("not-equal\n");
                > what is the output?
                > I am getting "not-equal" as output.
                > Why it is so?[/color]

                Short answer whould be "you should not use operator== to compare
                floating point values, unless you know what you are doing" The problem
                is floating point value is an approximation and you cannot use exact
                comparison on approximate values (actually you can but you will not get
                expected results). You should use something like if( fabs( v1 - v2 ) <=
                delta ) ... where delta depends on your application. If you need exact
                values you should use fixed point calculations and values. They are not
                directly supported by the language but you can easily find appropriate
                library for you.

                Regards,
                Vyacheslav

                Comment

                • Alf P. Steinbach

                  #9
                  Re: Calling a constructor from another

                  * Rolf Magnus:[color=blue]
                  >
                  > Still, even if you use those loop-hole constructs, you must ensure that each
                  > object only receives one constructor call if you want your program to
                  > behave in a defined way.[/color]

                  It's that imprecise use of language & logic that allows you to hold an
                  inconsistent belief, or at least, to argue for it.

                  Contrary to your statement, an object may receive _any_ number of
                  constructor calls in a well-defined program. Except if 'receive' is defined
                  so that all calls except one are excluded from being 'received', which is
                  then circular reasoning. I.e., your statement is extremely misleading,
                  asserting something that is false with a natural interpretation of the
                  language, and meaningless with an interpretation so that it's not false.

                  An object of class type T will, except for use of the mentioned constructs,
                  receive exactly one T constructor call.

                  --
                  A: Because it messes up the order in which people normally read text.
                  Q: Why is it such a bad thing?
                  A: Top-posting.
                  Q: What is the most annoying thing on usenet and in e-mail?

                  Comment

                  Working...