By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

syntax semantics: instantiation without arguments: parentheses

P: n/a

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

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

Sep 14 '06 #1
Share this Question
Share on Google+
3 Replies


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

Sep 14 '06 #2

P: n/a
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
Sep 14 '06 #3

P: n/a
Stuart Redmann <De*******@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
Sep 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.