No argument constructor and function prototype

Discussion in 'C++' started by bb, Jan 17, 2006.

  1. bb

    bb Guest

    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.
     
    bb, Jan 17, 2006
    #1
    1. Advertising

  2. bb wrote:
    > 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?


    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.

    >
    > }
    >
    > Thanks.
    >


    V
     
    Victor Bazarov, Jan 17, 2006
    #2
    1. Advertising

  3. bb

    Mike Wahler Guest

    "bb" <> wrote in message
    news:...
    > 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 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."

    > no
    > output. why?


    Prototypes do not generate output.

    > how to avoid it?


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

    -Mike
     
    Mike Wahler, Jan 18, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cybernerdsx2

    Function Prototype - different argument names

    cybernerdsx2, Jul 18, 2006, in forum: C Programming
    Replies:
    3
    Views:
    395
    Chris Torek
    Jul 18, 2006
  2. June Lee
    Replies:
    2
    Views:
    831
    Jim Cobban
    Apr 13, 2008
  3. Reckoner
    Replies:
    11
    Views:
    724
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    583
    Seebs
    Nov 21, 2011
  5. Replies:
    9
    Views:
    221
    Thomas 'PointedEars' Lahn
    May 26, 2006
Loading...

Share This Page