Constructor call

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cppquester@googlemail.com

    Constructor call

    What does this code do?

    #include <iostream>

    class A
    {
    public:
    A() { std::cout << "A::A()" << std::endl;}
    };

    int main( int argc, char* argv[])
    {
    A(); // constructor call?

    return 0;
    }

    I.e.
    a) What does this constructor call do?
    b) could this be used for somehing which makes sense?
    c) if not why is it allowed?

    Thanks,
    Marc

  • Markus Moll

    #2
    Re: Constructor call

    Hi

    cppquester@goog lemail.com wrote:
    What does this code do?
    >
    #include <iostream>
    >
    class A
    {
    public:
    A() { std::cout << "A::A()" << std::endl;}
    };
    >
    int main( int argc, char* argv[])
    {
    A(); // constructor call?
    >
    return 0;
    }
    >
    I.e.
    a) What does this constructor call do?
    It's not really a constructor call, but the creation of an unnamed
    temporary. An object of type A is created, not at all used, and then
    destroyed.
    b) could this be used for somehing which makes sense?
    What do you mean? The above example? That doesn't make sense. Creating
    unnamed temporaries like this in general is useful, consider:

    void func(const A&);
    func(A()); // Call func with a "default" A

    Otherwise you would have to give that A a name:

    void func(const A&);
    A a;
    func(a);
    c) if not why is it allowed?
    Why not? You could also write:

    int main()
    {
    5+5;
    }

    But that doesn't make any sense either. You can always write completely
    useless programs (at least in every sensible programming language).

    Markus

    Comment

    • Markus Moll

      #3
      Re: Constructor call

      Alf P. Steinbach wrote:
      * Markus Moll:
      >It's not really a constructor call,
      >
      Depends on whose terminology you adopt.
      >
      In the terminology used in the standard, and by the language's creator
      Bjarne Stroustrup[1] and people like Andrew Koenig and Nicolai Josuttis,
      it's an explicit constructor call.
      Um... okay, I wasn't aware of that (although this means I must have read it
      quite often). To me, "constructo r call" did not make as much sense, because
      it sounds like an invocation of the constructor is all that happens (which
      is of course not true). But good to know, I will refrain from "correcting "
      it in the future.

      Markus

      Comment

      • Jim Langston

        #4
        Re: Constructor call

        <cppquester@goo glemail.comwrot e in message
        news:1191315675 .365803.169790@ 57g2000hsv.goog legroups.com...
        What does this code do?
        >
        #include <iostream>
        >
        class A
        {
        public:
        A() { std::cout << "A::A()" << std::endl;}
        };
        >
        int main( int argc, char* argv[])
        {
        A(); // constructor call?
        >
        return 0;
        }
        >
        I.e.
        a) What does this constructor call do?
        Creates a temporary A instance.
        b) could this be used for somehing which makes sense?
        It could, but probably isn't. I could think of a scenario where someone
        might have a class constructor that does something useful but they wouldn't
        need to keep an instance around. I'm not sure if that would be good code or
        not though.
        c) if not why is it allowed?
        The same reason:

        1;

        is allowed.


        Comment

        • Puppet_Sock

          #5
          Re: Constructor call

          On Oct 2, 11:10 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
          [snips]
          a) What does this constructor call do?
          >
          Creates a temporary A instance.
          >
          b) could this be used for somehing which makes sense?
          >
          It could, but probably isn't. I could think of a scenario where someone
          might have a class constructor that does something useful but they wouldn't
          need to keep an instance around. I'm not sure if that would be good code or
          not though.
          Probably most of the "good" reasons for doing such
          are outside the standard language. For example, if
          you were controlling some hardware, you might want
          that temporary to do some prep work for you. Like,
          start up a motor or some such, and not return until
          the system gives the "motor at operating speed"
          message. That's a natural for a ctor in a temp.

          Probably most cases in the standard language are
          at least as easy and straightforward done in other
          ways.
          Socks

          Comment

          • Joe Greer

            #6
            Re: Constructor call

            Puppet_Sock <puppet_sock@ho tmail.comwrote in
            news:1191352905 .823326.27030@5 7g2000hsv.googl egroups.com:
            On Oct 2, 11:10 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
            [snips]
            a) What does this constructor call do?
            >>
            >Creates a temporary A instance.
            >>
            b) could this be used for somehing which makes sense?
            >>
            >It could, but probably isn't. I could think of a scenario where
            >someone might have a class constructor that does something useful but
            >they wouldn't need to keep an instance around. I'm not sure if that
            >would be good code or not though.
            >
            Probably most of the "good" reasons for doing such
            are outside the standard language. For example, if
            you were controlling some hardware, you might want
            that temporary to do some prep work for you. Like,
            start up a motor or some such, and not return until
            the system gives the "motor at operating speed"
            message. That's a natural for a ctor in a temp.
            >
            Probably most cases in the standard language are
            at least as easy and straightforward done in other
            ways.
            Socks
            >
            >
            It's hard to imagine a scenario where those aren't just functions in a
            namespace somewhere. Exactly why is it that you would want to create an
            object for that?

            joe

            Comment

            • James Kanze

              #7
              Re: Constructor call

              On Oct 2, 11:01 am, cppques...@goog lemail.com wrote:
              What does this code do?
              #include <iostream>
              class A
              {
              public:
              A() { std::cout << "A::A()" << std::endl;}
              };
              int main( int argc, char* argv[])
              {
              A(); // constructor call?
              return 0;
              }
              I.e.
              a) What does this constructor call do?
              It outputs "A::A()\n" to standard out.
              b) could this be used for somehing which makes sense?
              Hard to say. I've never used this exactly, but similar
              constructs do sometimes make sense.
              c) if not why is it allowed?
              What would you propose banning?

              --
              James Kanze (GABI Software) email:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • James Kanze

                #8
                Re: Constructor call

                On Oct 2, 11:58 am, "Alf P. Steinbach" <al...@start.no wrote:
                * Markus Moll:
                cppques...@goog lemail.com wrote:
                What does this code do?
                #include <iostream>
                class A
                {
                public:
                A() { std::cout << "A::A()" << std::endl;}
                };
                int main( int argc, char* argv[])
                {
                A(); // constructor call?
                return 0;
                }
                I.e.
                a) What does this constructor call do?
                It's not really a constructor call,
                Depends on whose terminology you adopt.
                In the terminology used in the standard, and by the language's
                creator Bjarne Stroustrup[1] and people like Andrew Koenig and
                Nicolai Josuttis, it's an explicit constructor call.
                According to the standard, it's a "Explicit type conversion
                (functional notation)". The semantics of this expression are
                specified to be "creates an rvalue of the specified type, which
                is value-initialized".

                Of course, informally, I'll call it a constructor call too (and
                formally, I find it awkward to speak of an explicit type
                conversion when there's nothing being converted). But for
                whatever reasons, that's not the language the standard uses in
                its formal definition of the construction.
                It is however a popular sport among some contributors to
                clc++, which group has even included some competent folks, to
                deny that that can make sense. Happily the numbers have
                dwindled. I'm using the authority argument above because
                that's the only one that's worked with them.
                The authority argument is IMHO the only one which can be made to
                work against what you are saying:-). Calling it an explicit
                type conversion doesn't make any sense. But that's what the
                standard does.

                The only explination I can think of as to why the standard uses
                such strange terminology is that 1) it doesn't want to talk
                about constructors for things like int (and of course, "int()"
                is another example of this type of expression), or 2) it doesn't
                want to treat the case with a single argument (i.e. "A(b)") as a
                special case (and of course, that case may call a user defined
                conversion operator on b, rather than the constructor of A).

                Of course, it is more than *just* a constructor call---the
                compiler also allocates memory for the object.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                Working...