(default) constructor/object initializing problem

Discussion in 'C++' started by Moritz Beller, Aug 30, 2004.

  1. Hi!

    Why does Object anObject; call the default constructor Object::Object(),
    but Object anObject(); doesn't? Instead, there really seems to be no
    call at all.

    best regards / Gruß
    Moritz Beller
    --
    web http://www.4momo.de
    mail momo dot beller at t-online dot de
    gpgkey http://gpg.notlong.com
    Moritz Beller, Aug 30, 2004
    #1
    1. Advertising

  2. Hi!

    "Moritz Beller" <> wrote in message
    news:cgv5rv$e4q$05$-online.com...
    Hi!

    Why does Object anObject; call the default constructor Object::Object(),
    but Object anObject(); doesn't? Instead, there really seems to be no
    call at all.

    Because "Object anObject();" looks like a function
    declaration to the compiler. Nothing is constructed
    really; instead the compiler will think that "anObject"
    is a function taking no arguments and returning an
    instance of "Object".

    - Risto -
    Risto Lankinen, Aug 30, 2004
    #2
    1. Advertising

  3. On Mon, 30 Aug 2004 12:23:18 GMT
    "Risto Lankinen" <> wrote:

    > Because "Object anObject();" looks like a function
    > declaration to the compiler. Nothing is constructed
    > really; instead the compiler will think that "anObject"
    > is a function taking no arguments and returning an
    > instance of "Object".
    >
    > - Risto -


    Thanks!

    best regards
    Moritz Beller
    --
    web http://www.4momo.de
    mail momo dot beller at t-online dot de
    gpgkey http://gpg.notlong.com
    Moritz Beller, Aug 30, 2004
    #3
  4. Moritz Beller

    JKop Guest

    Moritz Beller posted:

    > On Mon, 30 Aug 2004 12:23:18 GMT
    > "Risto Lankinen" <> wrote:
    >
    >> Because "Object anObject();" looks like a function
    >> declaration to the compiler. Nothing is constructed
    >> really; instead the compiler will think that "anObject"
    >> is a function taking no arguments and returning an
    >> instance of "Object".
    >>
    >> - Risto -


    Try compile the following:

    int main()
    {
    Object anObject1;

    anObject1.MemberFunc();


    Object anObject2();

    anObject2.MemberFunc();

    //Compile error, you can't have a fullstop
    //after a pointer. "anObject2" refers to a pointer
    //to the function. If you stick in brackets, as in:

    anObject().MemberFunc();

    //then the function will actually be called
    //and the fullstop will access members of the
    //object returned from the function

    }


    -JKop
    JKop, Sep 4, 2004
    #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. Aire
    Replies:
    3
    Views:
    465
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    960
    Alf P. Steinbach
    Mar 6, 2006
  3. Replies:
    4
    Views:
    415
    Salt_Peter
    Oct 12, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,225
  5. John
    Replies:
    22
    Views:
    791
    Francesco S. Carta
    Jun 25, 2010
Loading...

Share This Page