473,320 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

syntax semantics: instantiation without arguments: parentheses


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
3 1674
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
8
by: Ike | last post by:
I am hoping someone can help me with the proper syntax for this. I have an attribute, called, say "name," such that: <set name="something">thename</set> However, the value for name, is...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
21
by: Dmitry Anikin | last post by:
I mean, it's very convenient when default parameters can be in any position, like def a_func(x = 2, y = 1, z): ... (that defaults must go last is really a C++ quirk which is needed for overload...
3
by: Mark Berry | last post by:
Hi, I'm moving from VB6 to VB 2005. Two questions: 1. One book I'm using has syntax like Dim testConnection as SqlConnection = New SqlConnection(connectionString) However, the Help for...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
2
by: Andy Champ | last post by:
We have a class with something that, simplified, looks like this: template <typename Tclass foo { T beyondAllReaching; // In VC2005, this works OK template <typename Ufriend void...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.