Class declaration: TMember Member(); vs TMember Member;

Discussion in 'C++' started by Skybuck Flying, Jun 21, 2011.

  1. Hello,

    I have a question about the parenthesis (see *) and further consequences of
    it in the following example class:

    class TSomeClass
    {
    TSomeOtherClass mSomeOtherClass1(); // *
    TSomeOtherClass mSomeOtherClass2;

    void SomeRoutine();
    };


    void TSomeClass::SomeRoutine()
    {
    mSomeOtherClass1.SomeRoutine(); // does not compile
    // compiler error message:
    // error: nonstandard form for taking the address of a member function
    // error: expression must have class type

    mSomeOtherClass1->SomeRoutine(); // does not compile
    // error: nonstandard form for taking the address of a member function
    // error: expression must have pointer type


    mSomeOtherClass2.SomeRoutine(); // compiles just fine.
    }

    So what is the effect of the parenthesis at // *.

    To me it looked as if the default constructor for mSomeOtherClass1 was being
    called ?

    But perhaps something else is happening which causes the
    mSomeOtherClass2.SomeRoutine call to fail to compile ?!

    Does mSomeOtherClass2 need to be "constructed" by calling it's constructor
    in the default constructor of TSomeClass or is that automatic (so when
    parenthesis in the member declaration are left out) ?.

    Bye,
    Skybuck.
    Skybuck Flying, Jun 21, 2011
    #1
    1. Advertising

  2. On 6/21/2011 10:27 AM, Skybuck Flying wrote:
    > Hello,
    >
    > I have a question about the parenthesis (see *) and further consequences
    > of it in the following example class:
    >
    > class TSomeClass
    > {
    > TSomeOtherClass mSomeOtherClass1(); // *
    > TSomeOtherClass mSomeOtherClass2;
    >
    > void SomeRoutine();


    Explain to yourself the difference between

    sometype blah();
    and
    someothertype blahblah();

    and then explain to yourself the difference between

    TSomeOtherClass mBlah();

    and

    void SomeBlah();

    .. Perhaps after that you're going to see by yourself what the
    difference is between

    SomeType someName;

    and

    SomeType someName();

    (i.e. what the importance of the parentheses is).

    > };
    >
    >
    > void TSomeClass::SomeRoutine()
    > {
    > mSomeOtherClass1.SomeRoutine(); // does not compile
    > // compiler error message:
    > // error: nonstandard form for taking the address of a member function
    > // error: expression must have class type
    >
    > mSomeOtherClass1->SomeRoutine(); // does not compile
    > // error: nonstandard form for taking the address of a member function
    > // error: expression must have pointer type
    >
    >
    > mSomeOtherClass2.SomeRoutine(); // compiles just fine.
    > }
    >
    > So what is the effect of the parenthesis at // *.


    Read the FAQ section 10.

    > To me it looked as if the default constructor for mSomeOtherClass1 was
    > being called ?


    Yes, that's common among beginners, we've pretty much all been there.

    > But perhaps something else is happening which causes the
    > mSomeOtherClass2.SomeRoutine call to fail to compile ?!
    >
    > Does mSomeOtherClass2 need to be "constructed" by calling it's
    > constructor in the default constructor of TSomeClass or is that
    > automatic (so when parenthesis in the member declaration are left out) ?.
    >
    > Bye,
    > Skybuck.


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 21, 2011
    #2
    1. Advertising

  3. * Skybuck Flying, on 21.06.2011 16:27:
    > Hello,
    >
    > I have a question about the parenthesis (see *) and further consequences of it
    > in the following example class:
    >
    > class TSomeClass
    > {
    > TSomeOtherClass mSomeOtherClass1(); // *


    This is a function declaration (a declaration of a member function).


    > TSomeOtherClass mSomeOtherClass2;


    This is a variable declaration (a declaration of a member variable).


    > void SomeRoutine();
    > };



    [snip]

    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Jun 21, 2011
    #3
    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. Noah
    Replies:
    5
    Views:
    950
  2. E11
    Replies:
    1
    Views:
    4,737
    Thomas Weidenfeller
    Oct 12, 2005
  3. Alex Vinokur
    Replies:
    4
    Views:
    581
    Jonathan Turkanis
    Apr 5, 2004
  4. Ovidesvideo
    Replies:
    4
    Views:
    500
    Andrey Tarasevich
    Dec 10, 2004
  5. kelvSYC
    Replies:
    6
    Views:
    7,222
    Richard Herring
    May 17, 2005
Loading...

Share This Page