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

Discussion in 'C++' started by janzon@gmail.com, Jan 19, 2007.

  1. Guest

    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++
    , Jan 19, 2007
    #1
    1. Advertising

  2. hit_pc Guest

    On Fri, 19 Jan 2007 20:12:44 +0800, <> 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.
    hit_pc, Jan 19, 2007
    #2
    1. Advertising

  3. Rolf Magnus Guest

    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();".
    Rolf Magnus, Jan 19, 2007
    #3
  4. Alex Vinokur Guest

    Rolf Magnus wrote:
    > 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.lang.c /msg/0707f633d38f2f9f

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Jan 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,912
    Scott M.
    Oct 11, 2003
  2. Russ

    script versus code versus ?

    Russ, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,497
  3. Danny Castonguay

    Syntax: pointers versus value

    Danny Castonguay, Jul 30, 2003, in forum: Python
    Replies:
    5
    Views:
    288
    John Hunter
    Jul 30, 2003
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    251
    Christoffer Sawicki
    Sep 2, 2006
  5. Paul Butcher
    Replies:
    12
    Views:
    708
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page