John Goche wrote:
Quote:
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.
Quote:
std::cout << "two" << std::endl;
Foo *bar = new Foo();
Okay, but memory leak.
Quote:
std::cout << "three" << std::endl;
Foo hello;
Okay.
Quote:
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.
Source didn't compile because of missing return statement.
Quote:
----- output: ------------------------------
>
$ ./hello
one
hello
two
hello
three
hello
four
Regards,
Stuart