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
----------------------------------------------- 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".
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
| |