No argument constructor and function prototype

B

bb

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.
 
V

Victor Bazarov

bb said:
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
 
M

Mike Wahler

bb said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top