Is a constructor a function?

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

    Is a constructor a function?

    Hidden in another thread, the question has come up:

    "Is a constructor a function?"

    (My claim is "no", though some have disagreed.)

    Many thanks,
    --ag

    [Yes, I know what they do and how they are invoked. I'm trying to get an
    answer from a `language lawyer' perspective.]
    --
    Artie Gold -- Austin, Texas

  • Jerry Coffin

    #2
    Re: Is a constructor a function?

    In article <3F304D7A.40308 09@austin.rr.co m>, artiegold@austi n.rr.com
    says...[color=blue]
    > Hidden in another thread, the question has come up:
    >
    > "Is a constructor a function?"[/color]

    Yes, a ctor is a function, but (despite some appearances to the
    contrary) does not have a name. The relevant part of the standard is
    section 12.1, in case you care to look at it in more detail.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.

    Comment

    • Dave Rahardja

      #3
      Re: Is a constructor a function?

      On Wed, 06 Aug 2003 13:05:26 GMT, Jerry Coffin <jcoffin@taeus. com> wrote:
      [color=blue]
      >Yes, a ctor is a function, but (despite some appearances to the
      >contrary) does not have a name.[/color]

      Well, that's not entirely true. The ctor can't be called explicitly except
      during construction because it has no name visible to the code, but it does
      have a name through name mangling. i.e. the constructors

      class foo
      {
      public:
      foo();
      foo(int i);
      };

      are two different functions, and have distinctive names as far as the linker
      is concerned.

      Comment

      • John Dibling

        #4
        Re: Is a constructor a function?

        On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <artiegold@aust in.rr.com>
        wrote:
        [color=blue]
        >"Is a constructor a function?"
        >
        >(My claim is "no", though some have disagreed.)[/color]


        Yes, a constructor is a function. If a constructor weren't a
        function, then what exactly would it be?

        More to the point... 12:1: "The default constructor (12.1), copy
        constructor and copy assignment operator (12.8), and destructor (12.4)
        are special member functions."

        In addition, 12.1:1: "Constructo rs do not have names" and 12.1:2:
        "...Becasue constructors do not have names, they are never found
        during name lookup...," meaning they can't be called explicitly. Code
        such as:

        class A { /* ... */ } ;

        A();

        is legal because it is allowed by 12.1:13: "A functional notation type
        conversion can be used to create new objects of its type. [Note: The
        syntax looks like an erxplicit call of the constructor]"


        </dib>
        John Dibling
        Witty banter omitted for your protection

        Comment

        • Artie Gold

          #5
          Re: Is a constructor a function?

          John Dibling wrote:[color=blue]
          > On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <artiegold@aust in.rr.com>
          > wrote:
          >
          >[color=green]
          >>"Is a constructor a function?"
          >>
          >>(My claim is "no", though some have disagreed.)[/color]
          >
          >
          >
          > Yes, a constructor is a function. If a constructor weren't a
          > function, then what exactly would it be?
          >
          > More to the point... 12:1: "The default constructor (12.1), copy
          > constructor and copy assignment operator (12.8), and destructor (12.4)
          > are special member functions."
          >
          > In addition, 12.1:1: "Constructo rs do not have names" and 12.1:2:
          > "...Becasue constructors do not have names, they are never found
          > during name lookup...," meaning they can't be called explicitly. Code
          > such as:
          >
          > class A { /* ... */ } ;
          >
          > A();
          >
          > is legal because it is allowed by 12.1:13: "A functional notation type
          > conversion can be used to create new objects of its type. [Note: The
          > syntax looks like an erxplicit call of the constructor]"
          >
          >
          > </dib>
          > John Dibling
          > Witty banter omitted for your protection[/color]

          Thanks all. I wanted chapter and verse. I got chapter and verse.

          ;-)

          --ag

          --
          Artie Gold -- Austin, Texas

          Comment

          • Jerry Coffin

            #6
            Re: Is a constructor a function?

            In article <9m32jv4g3drnn5 jltden5jokbk04i phek8@4ax.com>, ask@me.com
            says...[color=blue]
            > On Wed, 06 Aug 2003 13:05:26 GMT, Jerry Coffin <jcoffin@taeus. com> wrote:
            >[color=green]
            > >Yes, a ctor is a function, but (despite some appearances to the
            > >contrary) does not have a name.[/color]
            >
            > Well, that's not entirely true.[/color]

            Yes, it is entirely true. The precise quote from the C++ standard
            (section 12.1/1, sentence 1) is: "Constructo rs do not have names."

            [ ... ]
            [color=blue]
            > foo();
            > foo(int i);
            > };
            >
            > are two different functions, and have distinctive names as far as the linker
            > is concerned.[/color]

            This has on relevance. When you compile and if/then/else statement, the
            output of the compiler will typically include (at least) a couple of
            named labels, but this doesn't mean that an if/then/else statement has a
            name -- only that the compiler typically uses some names to implement
            it. The same is true with ctors -- yes, the compiler typically
            synthesizes names for the pieces of code that implement them, but this
            means nothing about the ctors themselves.

            --
            Later,
            Jerry.

            The universe is a figment of its own imagination.

            Comment

            • Rolf Magnus

              #7
              Re: Is a constructor a function?

              John Dibling wrote:
              [color=blue]
              > On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <artiegold@aust in.rr.com>
              > wrote:
              >[color=green]
              >>"Is a constructor a function?"
              >>
              >>(My claim is "no", though some have disagreed.)[/color]
              >
              >
              > Yes, a constructor is a function. If a constructor weren't a
              > function, then what exactly would it be?[/color]

              It would be.... well, a constructor. If the C++ standard says that it
              sees a constructor as a function, then there is not much to say against
              it wrt. C++, but actually, that term isn't really correct, since a
              constructor is missing an important element that a function must have:
              a return type.

              Comment

              • Karl Heinz Buchegger

                #8
                Re: Is a constructor a function?



                Dave Rahardja wrote:[color=blue]
                >
                > On Wed, 06 Aug 2003 13:05:26 GMT, Jerry Coffin <jcoffin@taeus. com> wrote:
                >[color=green]
                > >Yes, a ctor is a function, but (despite some appearances to the
                > >contrary) does not have a name.[/color]
                >
                > Well, that's not entirely true. The ctor can't be called explicitly except
                > during construction because it has no name visible to the code, but it does
                > have a name through name mangling. i.e. the constructors
                >
                > class foo
                > {
                > public:
                > foo();
                > foo(int i);
                > };
                >
                > are two different functions, and have distinctive names as far as the linker
                > is concerned.[/color]

                The linker is not part of standard C++.
                In standard C++, a constructor has no name and thus cannot
                be called.

                --
                Karl Heinz Buchegger
                kbuchegg@gascad .at

                Comment

                • Mike Wahler

                  #9
                  Re: Is a constructor a function?

                  Artie Gold <artiegold@aust in.rr.com> wrote in message
                  news:3F311540.8 090602@austin.r r.com...[color=blue]
                  >
                  > Thanks all. I wanted chapter and verse. I got chapter and verse.[/color]

                  Remember, you can get your own copy of the "bible"
                  for only 18 dollars. http://tinyurl.com/31yw

                  -Mike



                  Comment

                  • Mike Wahler

                    #10
                    Re: Is a constructor a function?


                    Rolf Magnus <ramagnus@t-online.de> wrote in message
                    news:bgr6o9$3bm $04$1@news.t-online.com...[color=blue]
                    > John Dibling wrote:
                    >[color=green]
                    > > On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <artiegold@aust in.rr.com>
                    > > wrote:
                    > >[color=darkred]
                    > >>"Is a constructor a function?"
                    > >>
                    > >>(My claim is "no", though some have disagreed.)[/color]
                    > >
                    > >
                    > > Yes, a constructor is a function. If a constructor weren't a
                    > > function, then what exactly would it be?[/color]
                    >
                    > It would be.... well, a constructor. If the C++ standard says that it
                    > sees a constructor as a function, then there is not much to say against
                    > it wrt. C++, but actually, that term isn't really correct, since a
                    > constructor is missing an important element that a function must have:
                    > a return type.[/color]

                    Here's how I see it:

                    While the syntax for a ctor declaration does not
                    include a formal 'return type', it does return the type
                    of object for which the ctor was defined.

                    class T
                    {
                    int member;
                    public:
                    T(int arg) : member(arg) {}
                    };

                    T obj(42);

                    'obj' is initialized with value "returned"
                    by 'T::T()', (not with 42, as the syntax
                    seems to indicate.)

                    -Mike



                    Comment

                    • Dave Rahardja

                      #11
                      Re: Is a constructor a function?

                      On Wed, 06 Aug 2003 17:39:43 +0200, Karl Heinz Buchegger <kbuchegg@gasca d.at>
                      wrote:[color=blue]
                      >The linker is not part of standard C++.
                      >In standard C++, a constructor has no name and thus cannot
                      >be called.[/color]

                      Point taken!

                      Comment

                      • E. Robert Tisdale

                        #12
                        Re: Is a constructor a function?

                        Artie Gold wrote:
                        [color=blue]
                        > Hidden in another thread, the question has come up:
                        >
                        > "Is a constructor a function?"
                        >
                        > (My claim is "no", though some have disagreed.)[/color]

                        According to Bjarne Stroustrup,
                        "The C++ Programming Language: Third Edition",
                        Chapter2 A Tour of C++, Section 5 Data Abstraction,
                        Subsection 2 User-Defined Types, page 32:

                        "A member function with the same name as its class
                        is called a constructor. A constructor defines a way
                        to initialize an object of its class."

                        Subsection 3 Concrete Types, page 33:

                        "The constructor Stack(int) will be called
                        whenever an object of the class is created.
                        This takes care of initialization. "

                        Comment

                        • E. Robert Tisdale

                          #13
                          Re: Is a constructor a function?

                          Artie Gold wrote:
                          [color=blue]
                          > Hidden in another thread, the question has come up:
                          >
                          > "Is a constructor a function?"
                          >
                          > (My claim is "no", though some have disagreed.)[/color]

                          According to Bjarne Stroustrup,
                          "The C++ Programming Language: Third Edition",
                          Chapter2 A Tour of C++, Section 5 Data Abstraction,
                          Subsection 2 User-Defined Types, page 32:

                          "A member function with the same name as its class
                          is called a constructor. A constructor defines a way
                          to initialize an object of its class."

                          Subsection 3 Concrete Types, page 33:

                          "The constructor Stack(int) will be called
                          whenever an object of the class is created.
                          This takes care of initialization. "

                          Comment

                          Working...