Private constructor

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

    #31
    Re: Private constructor

    Andy wrote:
    [color=blue]
    > Constructors do have this pointers. Got that corrected. "This" can be
    > accessed inside the constructor body, not the initializer list ...
    > which is absolutely logical. In the initializer list, the object is
    > still to be completely constructed.[/color]

    The same may be said of the entire constructor, if the class has virtual
    methods.

    Thanks for helping explain why constructors cannot be called.

    -Jeff

    Comment

    • Mike Hewson

      #32
      Re: [Nit] Re: Private constructor

      "Ron Natalie" <ron@sensor.com > wrote in message
      news:3fe56b94$0 $472$9a6e19ea@n ews.newshosting .com...
      <snip>

      So, *evidently* it is the difference between implicit and explicit uses of
      the constructor ie. can I name it ( no choice really ) and call it by that
      name in the source code outside it's definition in the class ( no ), or will
      the implementation of the abstract/virtual machine invoke the constructor's
      code ( well, 'as if' anyway ) upon any given instantiation ( including
      temporaries, classes composing classes etc ... ) ?

      Cheers
      --
      Hewson::Mike
      "This letter is longer than usual because I lack the time to make it
      shorter" - Blaise Pascal


      Comment

      • Ron Natalie

        #33
        Re: [Nit] Re: Private constructor


        "Mike Hewson" <hewsmike@austa rnet.com.au> wrote in message news:bs683b$f3e $1@austar-news.austar.net .au...
        [color=blue]
        > So, *evidently* it is the difference between implicit and explicit uses of
        > the constructor ie. can I name it ( no choice really ) and call it by that
        > name in the source code outside it's definition in the class ( no )[/color]

        You can't name it. The standard says they don't have names just a
        convention for declaring/defining them. This is important as it resolves
        an ambiguity between whether it is a type name or the consturctor
        name (by eliminating the latter).

        Frankly, it's C++'s terseness and eschewing of additional reserved
        words.

        Since constructors NEVER participate in name resolution, there is
        NO way to reference it so it can be called. You always get a class
        name when you try so it always thinks that you are doing a cast.
        [color=blue]
        > or will
        > the implementation of the abstract/virtual machine invoke the constructor's
        > code ( well, 'as if' anyway ) upon any given instantiation ( including
        > temporaries, classes composing classes etc ... ) ?[/color]

        That is true. Any time you create an object either by invocation of new,
        defining a variable of that type, creation of a temporary, function arguments,
        subobjects of a larger object, etc... the compiler invokes the defined initialization
        on the object and all of its subobjects. For (most) classes this means invoking the constructor.
        So it is more than just one constructor being run in most cases.


        Comment

        • Mike Hewson

          #34
          Re: [Nit] Re: Private constructor

          "Ron Natalie" <ron@sensor.com > wrote in message
          news:3fe69e7a$0 $465$9a6e19ea@n ews.newshosting .com...[color=blue]
          > You can't name it. The standard says they don't have names just a
          > convention for declaring/defining them. This is important as it resolves
          > an ambiguity between whether it is a type name or the consturctor
          > name (by eliminating the latter). Frankly, it's C++'s terseness and[/color]
          eschewing[color=blue]
          > of additional reserved words. Since constructors NEVER participate in name
          > resolution, there is NO way to reference it so it can be called. You[/color]
          always get[color=blue]
          > a class name when you try so it always thinks that you are doing a cast.[/color]

          Bingo! That makes perfect sense, 'of course' .... I say in retrospect. :-)

          Thanks.

          Cheers
          --
          Hewson::Mike
          "This letter is longer than usual because I lack the time to make it
          shorter" - Blaise Pascal


          Comment

          • Ron Natalie

            #35
            Re: Private constructor


            "Jeff Schwab" <jeffplus@comca st.net> wrote in message news:q--dnabMl-wR8nui4p2dnA@co mcast.com...[color=blue]
            > Andy wrote:
            >[color=green]
            > > Constructors do have this pointers. Got that corrected. "This" can be
            > > accessed inside the constructor body, not the initializer list ...
            > > which is absolutely logical. In the initializer list, the object is
            > > still to be completely constructed.[/color]
            >
            > The same may be said of the entire constructor, if the class has virtual
            > methods.
            >[/color]
            Virtualness is hotwired to the type of the constructor's class while the constructor
            body is running. It's no more or less of a problem with virtual methods than
            anything else. It's always incumbent on code executing during construction
            to be aware of what has and has not been initialized so far.

            The only issue is that passing "this" outside the constructor context is a warning
            sign that someone ignorant of the fact that the object is still under construction
            may do something inappropriate. The compiler is just warning you to look
            further.

            Comment

            Working...