Connecting Tech Pros Worldwide Help | Site Map

syntax semantics: instantiation without arguments: parentheses

John Goche
Guest
 
Posts: n/a
#1: Sep 14 '06

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

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

David Harmon
Guest
 
Posts: n/a
#2: Sep 14 '06

re: syntax semantics: instantiation without arguments: parentheses


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

Stuart Redmann
Guest
 
Posts: n/a
#3: Sep 14 '06

re: syntax semantics: instantiation without arguments: parentheses


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.
Quote:
Foo bye();
}
Source didn't compile because of missing return statement.
Quote:
----- output: ------------------------------
>
$ ./hello
one
hello
two
hello
three
hello
four
Regards,
Stuart
Marcus Kwok
Guest
 
Posts: n/a
#4: Sep 14 '06

re: syntax semantics: instantiation without arguments: parentheses


Stuart Redmann <DerTopper@web.dewrote:
Quote:
John Goche wrote:
Quote:
>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
Closed Thread