syntax semantics: instantiation without arguments: parentheses

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

    syntax semantics: instantiation without arguments: parentheses


    Hello,

    I would like to know why the first three statements
    produce output whereas the last one does not.
    Also, should parentheses be included when
    constructing such an instance on the heap?
    What about on the stack? Why or why not.

    Thanks,

    JG

    --- input code: ------------------------------------

    #include <iostream>

    class Foo {

    public:

    Foo() {

    std::cout << "hello" << std::endl;

    }

    private:

    int x;

    };

    int main() {
    std::cout << "one" << std::endl;
    Foo *foo = new Foo;
    std::cout << "two" << std::endl;
    Foo *bar = new Foo();
    std::cout << "three" << std::endl;
    Foo hello;
    std::cout << "four" << std::endl;
    Foo bye();
    }

    ----- output: ------------------------------

    $ ./hello
    one
    hello
    two
    hello
    three
    hello
    four

    -----------------------------------------------

  • David Harmon

    #2
    Re: syntax semantics: instantiation without arguments: parentheses

    On 14 Sep 2006 04:39:30 -0700 in comp.lang.c++, "John Goche"
    <johngoche@gmai l.comwrote,
    > Foo bye();
    This is a declaration of a function named "bye" with no arguments
    and a return type of "Foo".

    Comment

    • Stuart Redmann

      #3
      Re: syntax semantics: instantiation without arguments: parentheses

      John Goche wrote:
      Hello,
      >
      I would like to know why the first three statements
      produce output whereas the last one does not.
      Also, should parentheses be included when
      constructing such an instance on the heap?
      What about on the stack? Why or why not.
      >
      Thanks,
      >
      JG
      >
      --- input code: ------------------------------------
      >
      #include <iostream>
      class Foo {
      public:
      Foo() {
      std::cout << "hello" << std::endl;
      }
      private:
      int x;
      };
      >
      int main() {
      std::cout << "one" << std::endl;
      Foo *foo = new Foo;
      Okay, but memory leak.
      std::cout << "two" << std::endl;
      Foo *bar = new Foo();
      Okay, but memory leak.
      std::cout << "three" << std::endl;
      Foo hello;

      Okay.
      std::cout << "four" << std::endl;
      This syntax means that you define a function bye that takes no arguments
      and returns a Foo. In contrast to dynamic creation, you must not supply
      an empty pair of parentheses in this case. To avoid confusion, I'd leave
      them out when using new, as well.
      Foo bye();
      }
      Source didn't compile because of missing return statement.
      ----- output: ------------------------------
      >
      $ ./hello
      one
      hello
      two
      hello
      three
      hello
      four
      Regards,
      Stuart

      Comment

      • Marcus Kwok

        #4
        Re: syntax semantics: instantiation without arguments: parentheses

        Stuart Redmann <DerTopper@web. dewrote:
        John Goche wrote:
        >int main() {
        > std::cout << "one" << std::endl;
        > Foo *foo = new Foo;
        > std::cout << "two" << std::endl;
        > Foo *bar = new Foo();
        > std::cout << "three" << std::endl;
        > Foo hello;
        > std::cout << "four" << std::endl;
        > Foo bye();
        >}
        >
        Source didn't compile because of missing return statement.
        You may be using an old compiler. In the new standard, the return
        statement in main() is optional, and will implicitly return 0 IIRC.

        --
        Marcus Kwok
        Replace 'invalid' with 'net' to reply

        Comment

        Working...