No argument constructor and function prototype

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

    #1

    No argument constructor and function prototype

    Hi,

    Class Test {
    public:
    Test() { cout << "Hello from Test" << endl; }
    ~Test() { cout << "Bye from Test" << endl; }
    };

    int main(int argc, char** argv) {

    Test t1; // Does call the constructor and i can see the output.
    Test t2(); // Compiler (mis)takes it for a function prototype? no
    output. why? how to avoid it?

    }

    Thanks.

  • Victor Bazarov

    #2
    Re: No argument constructor and function prototype

    bb wrote:[color=blue]
    > Class Test {
    > public:
    > Test() { cout << "Hello from Test" << endl; }
    > ~Test() { cout << "Bye from Test" << endl; }
    > };
    >
    > int main(int argc, char** argv) {
    >
    > Test t1; // Does call the constructor and i can see the output.
    > Test t2(); // Compiler (mis)takes it for a function prototype? no
    > output. why? how to avoid it?[/color]

    It's not a mistake. It's a function declaration. Why? Because such is
    the rule. Of the language. You just stepped in a puddle. No big deal.
    Remember where it is and don't step into it again.
    [color=blue]
    >
    > }
    >
    > Thanks.
    >[/color]

    V

    Comment

    • Mike Wahler

      #3
      Re: No argument constructor and function prototype


      "bb" <muralibala68@g mail.com> wrote in message
      news:1137535044 .318564.169010@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > Hi,
      >
      > Class Test {
      > public:
      > Test() { cout << "Hello from Test" << endl; }
      > ~Test() { cout << "Bye from Test" << endl; }
      > };
      >
      > int main(int argc, char** argv) {
      >
      > Test t1; // Does call the constructor and i can see the output.
      > Test t2(); // Compiler (mis)takes it for a function prototype?[/color]

      No mistake. It *is* a function prototype. It declares
      a function named 't2' which returns a type 'Test' value
      and has no parameters. The rule is "if it looks like a
      prototype, it is a prototype."
      [color=blue]
      > no
      > output. why?[/color]

      Prototypes do not generate output.
      [color=blue]
      > how to avoid it?[/color]

      Don't write a prototype if you mean something else.

      -Mike


      Comment

      Working...