Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ syntax, "MyClass m;" versus "MyClass m();"

Reply
Thread Tools

C++ syntax, "MyClass m;" versus "MyClass m();"

 
 
janzon@gmail.com
Guest
Posts: n/a
 
      01-19-2007
Hi!

When defining a new object m of class MyClass, with no parameters to
the constructor, one is supposed to write "MyClass m;". If one wants to
pass a parameter p to the constructor, one is supposed to write
"MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
wants to pass zero parameters. Why is that? It seems inelegant and
non-symmetric to me. What reason is there behind this?

/ Me, seeking insights into C++

 
Reply With Quote
 
 
 
 
hit_pc
Guest
Posts: n/a
 
      01-19-2007
On Fri, 19 Jan 2007 20:12:44 +0800, <(E-Mail Removed)> wrote:


> "MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
> wants to pass zero parameters. Why is that? It seems inelegant and

if you write "MyClass m()",
it is not a Class,it is a function declare,
it means:this is a function ,its name is m,its param is void,and it
returns sth with type MyClass.
think about this:
you can declare a function like this:int func(int a)
also you can :int func(int)



--
Hello,World!
----legolaskiss.
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi!
>
> When defining a new object m of class MyClass, with no parameters to
> the constructor, one is supposed to write "MyClass m;". If one wants to
> pass a parameter p to the constructor, one is supposed to write
> "MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
> wants to pass zero parameters. Why is that? It seems inelegant and
> non-symmetric to me. What reason is there behind this?


You're right in that it is kind of unintuitive. The reason is that
"MyClass m();" already has another meaning. It declares a function taking no
arguments and returnint MyClass, similar to "int myfunc();".

 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      01-19-2007

Rolf Magnus wrote:
> (E-Mail Removed) wrote:
>
> > Hi!
> >
> > When defining a new object m of class MyClass, with no parameters to
> > the constructor, one is supposed to write "MyClass m;". If one wants to
> > pass a parameter p to the constructor, one is supposed to write
> > "MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
> > wants to pass zero parameters. Why is that? It seems inelegant and
> > non-symmetric to me. What reason is there behind this?

>
> You're right in that it is kind of unintuitive. The reason is that
> "MyClass m();" already has another meaning. It declares a function taking no
> arguments and returnint MyClass, similar to "int myfunc();".


See a message related to the issue:
http://groups.google.com/group/comp....07f633d38f2f9f

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
Certification - MCP versus MCSA versus MCSE =?Utf-8?B?Q2FuYWRhX0d1eU0=?= Microsoft Certification 1 08-24-2006 06:33 PM
script versus code versus ? Russ ASP .Net 1 06-10-2004 03:06 AM
HTML Client Control versus. HTML Server Control versus. Web Server Control Matthew Louden ASP .Net 1 10-11-2003 07:09 PM



Advertisments