Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Class declaration: TMember Member(); vs TMember Member;

Reply
Thread Tools

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

 
 
Skybuck Flying
Guest
Posts: n/a
 
      06-21-2011
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-21-2011
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      06-21-2011
* 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>
 
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
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
instanciate a class in a jar file with class.forname, while my main class is in another jar cyril Java 2 08-25-2004 06:55 AM



Advertisments