Private constructor

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

    #16
    Re: [Nit] Re: Private constructor


    "Jeff Schwab" <jeffplus@comca st.net> wrote in message
    news:j9CdnWsk3d gaB3miRVn-vA@comcast.com. ..[color=blue]
    > Ron Natalie wrote:[color=green]
    > > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
    > > news:Lc-dnSQxNPnx8HmiRV n-ig@comcast.com. ..
    > >
    > >[color=darkred]
    > >>>I have no clue what you are talking about. Constructors are[/color][/color][/color]
    non-static[color=blue][color=green][color=darkred]
    > >>>member functions. You can't call them.
    > >>
    > >><nit> Yes, you can. Allocators do it all the time. </nit>[/color]
    > >
    > >
    > > Sorry, totally wrong. They do not participate in name resolution, they
    > > can't be called. Allocators don't do it.[/color]
    >
    > I don't know what you mean. Is there a technicality that makes my use
    > of "call" incorrect? Where should I look for an explanation?[/color]
    Yes, there is no way to call a constructor. They don't have names, the do
    not
    participate in name resolution. You can NOT call them. They are called
    by
    the implementation as a side effect of creating an object.


    Comment

    • Alf P. Steinbach

      #17
      Re: [Nit] Re: Private constructor

      On Sat, 20 Dec 2003 15:09:45 -0500, "Ron Natalie" <ron@sensor.com > wrote:
      [color=blue]
      >
      >"Alf P. Steinbach" <alfps@start.no > wrote in message
      >news:3fe4723e. 517707687@News. CIS.DFN.DE...
      >[color=green]
      >> Perhaps it's just a question of terminology.[/color]
      >
      >Perhaps, but it appears yours is not accureate.[color=green]
      >>
      >> I find it more useful to use the word "call" about any statement that
      >> causes a function to be executed, with control returning to the next
      >> statement.
      >>
      >> T();[/color]
      >
      >If T is a type name, this is not a fucntion call. It is only a function
      >call, when T has function type.[/color]

      If you don't think a constructor is a member function, then consult the
      standard. Chapter 12 is about special member functions. Do you think
      something can be a member function and not a function?


      [color=blue]
      >When T is a type name, you have just created a default rvalue of that type.[/color]

      That's what I wrote, yes. In this you are correct.


      [color=blue][color=green]
      >> is a simple example of executing the default constructor of type T,
      >> here on a temporary object.[/color]
      >
      >No, it creates a temporary object.[/color]

      Where did you get the idea that there is any conflict between
      calling a constructor and creating an object?

      The main purpose of a constructor is exactly to _strongly couple_
      those two actions.


      [color=blue]
      >The constructor is invoked when ever an object that has one is created[/color]

      What is, in your opinion, the difference between "invoked", "executed"
      and "called"?

      [color=blue][color=green]
      >>
      >> new(place) T();
      >>
      >> is a simple example of executing the default constructor of type T on
      >> a given area of memory 'place'.[/color]
      >
      >No,[/color]

      Oh yes.

      [color=blue]
      > it creates an object using the placement new syntax,[/color]

      Yes, it does.

      [color=blue]
      > which means that the placement allocation function is called[/color]

      There isn't necessarily a placement allocation function.


      [color=blue]
      >and then the constructor is invoked on that.[/color]

      Is called, yes. ;-)

      [color=blue]
      >
      >In neither case are you calling the constructor,[/color]

      Oh yes I am.

      [color=blue]
      > you are causing objects to be created[/color]

      Yes I am.

      [color=blue]
      >and the constructor is called by the environment as a side effect.[/color]

      That is not meaningful in any way.


      [color=blue][color=green]
      >> In pure C++ terminology one might elect to call the latter a placement
      >> new, and have no problems with multiple meanings of words.[/color]
      >
      >The meaning of the words is quite well defined by the standard. The
      >constructor doesn't have a name. It does not participate in name
      >resolution. You can't call it,[/color]

      Oh yes I can, as demonstrated.

      [color=blue]
      > you can't get a pointer to it, you can't do anything with it
      >other than define it and let the the implementation call it at
      >the appropriate times.[/color]

      Oh yes I can, and it's very simple: placement new is in the language
      exactly for that purpose.


      [color=blue][color=green]
      >> But how would you then describe it to, say, an Ada programmer?[/color]
      >
      >Like I just did.[/color]

      You haven't.

      Comment

      • Jeff Schwab

        #18
        Re: [Nit] Re: Private constructor

        Ron Natalie wrote:[color=blue]
        > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
        > news:j9CdnWsk3d gaB3miRVn-vA@comcast.com. ..
        >[color=green]
        >>Ron Natalie wrote:
        >>[color=darkred]
        >>>"Jeff Schwab" <jeffplus@comca st.net> wrote in message
        >>>news:Lc-dnSQxNPnx8HmiRV n-ig@comcast.com. ..
        >>>
        >>>
        >>>
        >>>>>I have no clue what you are talking about. Constructors are[/color][/color]
        >
        > non-static
        >[color=green][color=darkred]
        >>>>>member functions. You can't call them.
        >>>>
        >>>><nit> Yes, you can. Allocators do it all the time. </nit>
        >>>
        >>>
        >>>Sorry, totally wrong. They do not participate in name resolution, they
        >>>can't be called. Allocators don't do it.[/color]
        >>
        >>I don't know what you mean. Is there a technicality that makes my use
        >>of "call" incorrect? Where should I look for an explanation?[/color]
        >
        > Yes, there is no way to call a constructor. They don't have names, the do
        > not
        > participate in name resolution. You can NOT call them. They are called
        > by
        > the implementation as a side effect of creating an object.[/color]

        You said that already. I don't follow. One more time: Where should I
        look for an explanation?

        Comment

        • Ron Natalie

          #19
          Re: [Nit] Re: Private constructor


          "Alf P. Steinbach" <alfps@start.no > wrote in message
          news:3fe4acdd.5 32715250@News.C IS.DFN.DE...[color=blue]
          > On Sat, 20 Dec 2003 15:09:45 -0500, "Ron Natalie" <ron@sensor.com > wrote:
          >[color=green]
          > >
          > >If T is a type name, this is not a fucntion call. It is only a function
          > >call, when T has function type.[/color]
          >
          > If you don't think a constructor is a member function, then consult the
          > standard. Chapter 12 is about special member functions. Do you think
          > something can be a member function and not a function?
          >[/color]

          I never said the constructor wasn't a member function. I said if T in your
          example was a TYPE NAME then it is not a function call. It's not the
          syntax for a function call. And if you would bother reading Chapter 12
          about
          special member functions, you will find almost word for word the description
          I gave you about the constructor NOT HAVING A NAME.
          [color=blue][color=green]
          >> Where did you get the idea that there is any conflict between[/color]
          > calling a constructor and creating an object?[/color]

          What conflict. They are two different concepts. The constructor invocation
          is part of the object creation, but you are not "calling" it with the syntax
          you
          gave. You need to look at the syntax for expressions.
          TYPEID ( )
          is an explicit type conversion (5.2.3 of the standard).
          It is NOT a function call (5.2.2 of the standard),.
          [color=blue][color=green][color=darkred]
          >> >The constructor is invoked when ever an object that has one is created[/color][/color]
          >
          > What is, in your opinion, the difference between "invoked", "executed">[/color]

          It is called by the implemenation, not by the programmer. I specifically
          said
          that, but you consider to ignore it in your refusal to admit that you can't
          understand
          the first sentence of 12.1[color=blue]
          >
          >[color=green]
          > > which means that the placement allocation function is called[/color]
          >
          > There isn't necessarily a placement allocation function.[/color]

          Then the program is ill-formed. In order for the syntax you wrote
          to work, you must define the placement allocation function (or #include
          <new>
          which does it for you).
          [color=blue][color=green]
          > >In neither case are you calling the constructor,[/color]
          >
          > Oh yes I am.
          >[/color]
          Please go back and read 5.2.2, 5.2.3, and 12.1.
          [color=blue]
          >[color=green]
          > > you can't get a pointer to it, you can't do anything with it
          > >other than define it and let the the implementation call it at
          > >the appropriate times.[/color]
          >
          > Oh yes I can, and it's very simple: placement new is in the language
          > exactly for that purpose.[/color]

          ??? How do you get a potiner to the constructor.



          Comment

          • Ron Natalie

            #20
            Re: [Nit] Re: Private constructor


            "Jeff Schwab" <jeffplus@comca st.net> wrote in message
            news:7oGdnRpfNp TlLHmiRVn-hA@comcast.com. ..
            [color=blue]
            > You said that already. I don't follow. One more time: Where should I
            > look for an explanation?
            >[/color]
            5.2.2 and 5.2.3 show the syntax for function calls versus type conversion.

            Given the statement :
            T();

            If T is a type id, then the expression is a type conversion (5.2.3),
            otherwise
            it has to be function name (5.2.2) to be a function call.

            12.1 in the very first sentence details that constructors do not have names
            and can not be called.


            Comment

            • Alf P. Steinbach

              #21
              Re: [Nit] Re: Private constructor

              On Sat, 20 Dec 2003 15:34:07 -0500, "Ron Natalie" <ron@sensor.com > wrote:
              [color=blue]
              >And if you would bother reading Chapter 12 about
              >special member functions, you will find almost word for word the description
              >I gave you about the constructor NOT HAVING A NAME.[/color]

              The standard uses the word "call" about calling a constructor.

              Comment

              • Ron Natalie

                #22
                Re: [Nit] Re: Private constructor


                "Alf P. Steinbach" <alfps@start.no > wrote in message
                news:3fe4be68.5 37206218@News.C IS.DFN.DE...[color=blue]
                > On Sat, 20 Dec 2003 15:34:07 -0500, "Ron Natalie" <ron@sensor.com > wrote:
                >[color=green]
                > >And if you would bother reading Chapter 12 about
                > >special member functions, you will find almost word for word the[/color][/color]
                description[color=blue][color=green]
                > >I gave you about the constructor NOT HAVING A NAME.[/color]
                >
                > The standard uses the word "call" about calling a constructor.[/color]
                It doesn't say that T() is a call of a constructor nor does it say anywhere
                that
                a program can call the constructor directly. It specifically states
                otherwise.

                You were the one who started this inane subthread by inserting the blatantly
                false statement in response to my attempt to help another user. Grow up.


                Comment

                • Alf P. Steinbach

                  #23
                  Re: [Nit] Re: Private constructor

                  On Sat, 20 Dec 2003 21:52:17 -0500, "Ron Natalie" <ron@sensor.com > wrote:
                  [color=blue]
                  >
                  >"Alf P. Steinbach" <alfps@start.no > wrote in message
                  >news:3fe4be68. 537206218@News. CIS.DFN.DE...[color=green]
                  >> On Sat, 20 Dec 2003 15:34:07 -0500, "Ron Natalie" <ron@sensor.com > wrote:
                  >>[color=darkred]
                  >> >And if you would bother reading Chapter 12 about
                  >> >special member functions, you will find almost word for word the[/color][/color]
                  >description[color=green][color=darkred]
                  >> >I gave you about the constructor NOT HAVING A NAME.[/color]
                  >>
                  >> The standard uses the word "call" about calling a constructor.[/color]
                  >
                  >It doesn't say that T() is a call of a constructor[/color]

                  See e.g. §12.1/5: "A default constructor for a class X is a constructor
                  of class X that can be called without an argument".
                  ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^

                  Also many other paragraphs that concern constructor _calls_.


                  [color=blue]
                  >nor does it say anywhere that
                  >a program can call the constructor directly. It specifically states
                  >otherwise.[/color]

                  If you think so, submit a defect report.

                  What you cannot do, however, is to call a constructor without
                  constructing an object (or at least, attempting to do so).

                  On the third hand, you can specify the storage in which to
                  construct the object.

                  [color=blue]
                  >You were the one who started this inane subthread by inserting the blatantly
                  >false statement in response to my attempt to help another user. Grow up.[/color]

                  Ron, Ron, it's almost Christmas, and this is only _terminology_, for
                  Crissake!

                  If that's what's needed to please you I'll renounce the standard once
                  and for all: it's false, utterly false, and in contrast to what §12.1/5
                  falsely asserts, you cannot _call_ a default constructor (or any
                  constructor, for that matter), with or without an argument. There! OK?

                  PS: I'm leaving for my Christmas Holiday in a few hours, so I may not be
                  able to reply to any response from you. But hopefully the standard is
                  good enough? If not, whoa, we'll have to fix it! ;-)

                  Happy Christmas, and Merry New Year to you, and all other participants
                  in this group!

                  Cheers!

                  Comment

                  • Ron Natalie

                    #24
                    Re: [Nit] Re: Private constructor


                    "Alf P. Steinbach" <alfps@start.no > wrote in message
                    news:3fe517d2.5 60096265@News.C IS.DFN.DE...
                    [color=blue]
                    >
                    > See e.g. §12.1/5: "A default constructor for a class X is a constructor
                    > of class X that can be called without an argument".
                    > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^[/color]

                    Yes but it doesn't say anything about the programmer being able to call it.

                    You are ignoring the rest of 12.1.[color=blue]
                    >
                    > Also many other paragraphs that concern constructor _calls_.
                    >
                    >
                    >[color=green]
                    > >nor does it say anywhere that
                    > >a program can call the constructor directly. It specifically states
                    > >otherwise.[/color]
                    >
                    > If you think so, submit a defect report.[/color]

                    The only defect is your insistance on reading things into the standard
                    which are not there.[color=blue]
                    >
                    > What you cannot do, however, is to call a constructor without
                    > constructing an object (or at least, attempting to do so).[/color]

                    No, what you can not do is call the constructor. You can only create
                    objects, and in that case the implementation invokes the constructors
                    at the appropriate time.
                    [color=blue]
                    > Ron, Ron, it's almost Christmas, and this is only _terminology_, for
                    > Crissake![/color]

                    It is not just terminology. The original poster was quite confused (and
                    further confused by your misinformation) . The syntax presented is not
                    a call of a constructor, it is the creation of the object. If it were a
                    member function call to the constructor, it would be invoked on the
                    "this" object, which it is not. This is a common confusion newbies
                    make. There is no syntax to directly call constructors.
                    [color=blue]
                    >
                    > If that's what's needed to please you I'll renounce the standard once
                    > and for all: it's false, utterly false, and in contrast to what §12.1/5
                    > falsely asserts, you cannot _call_ a default constructor (or any
                    > constructor, for that matter), with or without an argument. There! OK?[/color]

                    Correct, you can not call constructors, NOTHING THE STANDARD SAYS
                    IS IN CONFLICT WITH THIS.

                    You seem to refuse to understand the difference between a program calling
                    a constuctor and it being called by the implementation.


                    Comment

                    • Andy

                      #25
                      Re: Private constructor

                      "Ron Natalie" <ron@sensor.com > wrote in message news:<3fe46ca3$ 0$70048$9a6e19e a@news.newshost ing.com>...[color=blue]
                      > "Andy" <garth_rockett@ yahoo.com> wrote in message
                      > news:96dbbc80.0 312200331.50986 22c@posting.goo gle.com...[color=green]
                      > > <
                      > > I am sorry I made a stupid mistake. I was under the impression that
                      > > just as static functions cannot access instance data members of a
                      > > class, they also cannot call non-static member functions. Of course
                      > > member functions are not per instance even if they are made to look
                      > > that way. Constructor being a special kind of such a function should
                      > > be accessible from static member functions.[color=darkred]
                      > > >[/color]
                      > >
                      > > That was stupider on second thoughts. A constructor does not get a
                      > > this pointer secretly. So it's not a special kind of the others ... it
                      > > is special and different from the rest. Nevertheless, I think I am
                      > > right in assuming that neither the member functions nor the
                      > > constructor or destructor constitute the state of an instantiated
                      > > object - therefore static member functions should be able to access
                      > > them[/color]
                      >
                      > I have no clue what you are talking about. Constructors are non-static
                      > member functions. You can't call them. They do have this pointers.[/color]

                      The constructor can indeed not be called. Of course the CRT Startup
                      code calls the constructor on behalf of the instantiator.

                      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=blue]
                      > You're confusing access with instantiation. A static member function
                      > has access to the private members of other objects of the same class
                      > just like non-static members of one object can access private
                      > members of other instances of the class.[/color]

                      Can static members access any non-static data members of an object of
                      a class? Don't think so. They can of course call non-static member
                      functions.

                      //// try compiling this code
                      //// It does not compile
                      #include <iostream>

                      class Cabc
                      {
                      private:
                      int abc;
                      int d;

                      public:
                      Cabc(int a) : abc(a)
                      {
                      std::cout<<this->abc<<std::endl ;
                      }

                      static int getValue()
                      {
                      return abc;
                      }
                      };

                      int main()
                      {
                      Cabc(19);
                      return 0;
                      }

                      /////////////////////////////////////////////////////////////////////////

                      Cheers,
                      Andy

                      Comment

                      • Andy

                        #26
                        Re: Private constructor

                        > A static member function[color=blue]
                        > has access to the private members of other objects of the same class
                        > just like non-static members of one object can access private
                        > members of other instances of the class.[/color]

                        I think the following is true:

                        1) Static member functions cannot access any non-static data members
                        of a class.
                        2) Static member functions cannot call any non-static member functions
                        of a class as a logical corollary to 1). Because these non-static
                        member functions themselves might be accessing non-static data
                        members.

                        Am I missing anything?

                        With constructors, this is different although they are non-static. The
                        static member function can instantiate an object of the class it
                        belongs to. This is what I found out.

                        Comment

                        • Rob Williscroft

                          #27
                          Re: Private constructor

                          Andy wrote in news:96dbbc80.0 312210922.25cb2 e27@posting.goo gle.com:
                          [color=blue]
                          > Can static members access any non-static data members of an object of
                          > a class? Don't think so. They can of course call non-static member
                          > functions.
                          >
                          > //// try compiling this code
                          > //// It does not compile
                          > #include <iostream>
                          >
                          > class Cabc
                          > {
                          > private:
                          > int abc;
                          > int d;
                          >
                          > public:
                          > Cabc(int a) : abc(a)
                          > {
                          > std::cout<<this->abc<<std::endl ;
                          > }
                          >[/color]

                          static int getValue( Cabc const &c )
                          {
                          return c.abc;
                          }
                          [color=blue]
                          > };
                          >
                          > int main()
                          > {[/color]

                          Cabc c(19);
                          std::cout << Cabc::getValue( c ) << std::endl;
                          [color=blue]
                          > }[/color]

                          The problem wasn't member access, it was the lack of an object.

                          Rob.
                          --

                          Comment

                          • Andy

                            #28
                            Re: [Nit] Re: Private constructor

                            Jeff Schwab <jeffplus@comca st.net> wrote in message news:<7oGdnRpfN pTlLHmiRVn-hA@comcast.com> ...[color=blue]
                            > Ron Natalie wrote:[color=green]
                            > > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                            > > news:j9CdnWsk3d gaB3miRVn-vA@comcast.com. ..
                            > >[color=darkred]
                            > >>Ron Natalie wrote:
                            > >>
                            > >>>"Jeff Schwab" <jeffplus@comca st.net> wrote in message
                            > >>>news:Lc-dnSQxNPnx8HmiRV n-ig@comcast.com. ..
                            > >>>
                            > >>>
                            > >>>
                            > >>>>>I have no clue what you are talking about. Constructors are[/color]
                            > >
                            > > non-static
                            > >[color=darkred]
                            > >>>>>member functions. You can't call them.
                            > >>>>
                            > >>>><nit> Yes, you can. Allocators do it all the time. </nit>
                            > >>>
                            > >>>
                            > >>>Sorry, totally wrong. They do not participate in name resolution, they
                            > >>>can't be called. Allocators don't do it.
                            > >>
                            > >>I don't know what you mean. Is there a technicality that makes my use
                            > >>of "call" incorrect? Where should I look for an explanation?[/color]
                            > >
                            > > Yes, there is no way to call a constructor. They don't have names, the do
                            > > not
                            > > participate in name resolution. You can NOT call them. They are called
                            > > by
                            > > the implementation as a side effect of creating an object.[/color]
                            >
                            > You said that already. I don't follow. One more time: Where should I
                            > look for an explanation?[/color]

                            I feel sorry I started a thread which gave rise to this (otherwise
                            increasingly colourful) sub-thread.

                            The constructor of an object _cannot_ be called by a code creating the
                            object. This might make for a good war on terminology ... but calling
                            a member function and calling a constructor are not the same thing.
                            The constructor of all objects will be invoked by the C++ Runtime
                            startup code.

                            For one, you cannot keep "calling" the constructor of an instance
                            because it makes no sense. You can do so with member functions (I know
                            that was not needed to be mentioned).

                            Also please refer to Scott Meyers'** discussion on overloading
                            "operator new" and "operator delete". He mentions that you cannot
                            overload the "new operator" which is meant to construct the object ...
                            bring it to life. He distinguishes it from the "operator new" (a
                            convenient terminology) which can be overloaded to take care of your
                            memory allocation scheme.

                            ----
                            ** Don't remember which of the two books: Effective or More Effective
                            C++.

                            Cheers,
                            Andy

                            Comment

                            • Ron Natalie

                              #29
                              Re: Private constructor


                              "Andy" <garth_rockett@ yahoo.com> wrote in message news:96dbbc80.0 312210922.25cb2 e27@posting.goo gle.com...
                              [color=blue]
                              >[color=green]
                              > > You're confusing access with instantiation. A static member function
                              > > has access to the private members of other objects of the same class
                              > > just like non-static members of one object can access private
                              > > members of other instances of the class.[/color]
                              >
                              > Can static members access any non-static data members of an object of
                              > a class? Don't think so. They can of course call non-static member
                              > functions.
                              >
                              > //// try compiling this code
                              > //// It does not compile[/color]

                              It has different problems. You're confusing non-static and static with
                              public versus private.

                              Consider this:

                              class A {
                              int i; // private

                              public:
                              A(int i) : i(i) { }
                              static int StaticGetA(A& ar) { return ar.i; } // legal!
                              };

                              int NonMemberGetA(A & ar) { return ar.i; } // not legal, doesn't compile

                              A a(5);

                              cout << a.i; // Illegal, doesn't compile.
                              cout << StaticGetA(a); // returns 5

                              See the StaticGetA has access to A::i where none of the external fucntions do.

                              Comment

                              • Ron Natalie

                                #30
                                Re: Private constructor


                                "Andy" <garth_rockett@ yahoo.com> wrote in message news:96dbbc80.0 312210934.4b902 a49@posting.goo gle.com...
                                [color=blue]
                                > 1) Static member functions cannot access any non-static data members
                                > of a class.[/color]

                                They can if they access it through a pointer or a reference. You are right, they
                                can't without as they have no "this" object to get the member from. However, given
                                an object pointer they can get access to the private non-static members on the object.
                                [color=blue]
                                >
                                > With constructors, this is different although they are non-static. The
                                > static member function can instantiate an object of the class it
                                > belongs to. This is what I found out.[/color]

                                It's not really that different. The static member can create an object of the
                                type because the constructor is ACCESSIBLE (i.e., the private/protected/public
                                rules).

                                You see there are two different principles here:

                                static versus non-static (I don't know what to call this distinction in general).
                                Access control (which is the public/private/static protected rules).


                                Comment

                                Working...