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

S

Skybuck Flying

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.
 
V

Victor Bazarov

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
 
A

Alf P. Steinbach /Usenet

* 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top