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

difference between MyClass a(); and MyClass a;

P: n/a
What is the difference between the two lines in the main function?

class MyClass{ ....};

int main(){
MyClass a();
MyClass a;
}
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Gunnar G" <de****@comhem.se> wrote in message
news:Y3*******************@newsb.telia.net...
What is the difference between the two lines in the main function?

class MyClass{ ....};

int main(){
MyClass a();
MyClass a;
}

Look it up. It's in section 10 of the FAQ:
http://www.parashift.com/c++-faq-lite/

(Hint: what does "int foo();" mean to you?)

-Howard
Jul 23 '05 #2

P: n/a
On 2005-07-07 23:38:20 +0100, "Howard" <al*****@hotmail.com> said:
"Gunnar G" <de****@comhem.se> wrote in message
news:Y3*******************@newsb.telia.net...
What is the difference between the two lines in the main function?

class MyClass{ ....};

int main(){
MyClass a();
MyClass a;
}


Look it up. It's in section 10 of the FAQ:
http://www.parashift.com/c++-faq-lite/

(Hint: what does "int foo();" mean to you?)


I for one, would have gladly traded that "C compatibility" quirk,
and happily lived in a world where C++ would have disallowed function
delclarations _within_ function definitions ...

Stroustrup once wrote that "C arrays are broken beyond repair", and
I'm quite close to think the same holds for C++ ... beyond repair.

Not to say that C++ is useless, or whatever, just unncessarily
inconsistent, as soon as you start looking at the details.

I wish Stroustrup had come up with: "--C" instead :-)
--
JFB

Jul 23 '05 #3

P: n/a
MyClass a();

It declares a function a() which returns MyClass type Object.

Jul 23 '05 #4

P: n/a
ibm
How about some happy medium.
Say: +C-
Call it "C(More or Less)"

IBM

Jul 23 '05 #5

P: n/a

"verec" <ve***@mac.com> wrote in message
news:42***********************@news.aaisp.net.uk.. .
On 2005-07-07 23:38:20 +0100, "Howard" <al*****@hotmail.com> said:
"Gunnar G" <de****@comhem.se> wrote in message
news:Y3*******************@newsb.telia.net...
What is the difference between the two lines in the main function?

class MyClass{ ....};

int main(){
MyClass a();
MyClass a;
}


Look it up. It's in section 10 of the FAQ:
http://www.parashift.com/c++-faq-lite/

(Hint: what does "int foo();" mean to you?)


I for one, would have gladly traded that "C compatibility" quirk,
and happily lived in a world where C++ would have disallowed function
delclarations _within_ function definitions ...

Stroustrup once wrote that "C arrays are broken beyond repair", and
I'm quite close to think the same holds for C++ ... beyond repair.

Not to say that C++ is useless, or whatever, just unncessarily
inconsistent, as soon as you start looking at the details.

I wish Stroustrup had come up with: "--C" instead :-)
--
JFB


I don't see what the problem is here. The following is a normal function
prototype:

int foo();

How else would you declare a function foo that returns an int?

Likewise, the following is also simply a function prototype:

MyClass foo();

Again, how else could you declare a function foo that returns a MyClass
object?

There's no "C compatibility quirk" here that I can see. It's perfectly
logical, and consistent.

You just need to learn to read "MyClass foo;" as a variable declaration and
"MyClass foo();" as a function declaration. The parentheses tell you that.

Whether or not the syntax is allowed inside another function is irrelevant.
You wouldn't want a function prototype to suddenly be seen as a variable
declaration just because it's inside a function, would you? THAT would be
inconsistent and confusing!

Now granted, there ARE situations where the distinction may not be so easily
seen. (See FAQ 10.19 for example.) But I'd hardly say C++ is "broken
beyond repair". It just takes a while to learn the details. You can always
use Delphi or C# if you don't like C++. Of course, you'll probably need to
look for a new job... :-)

-Howard

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.