(default) constructor/object initializing problem

M

Moritz Beller

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
 
R

Risto Lankinen

Hi!

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 -
 
M

Moritz Beller

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
 
J

JKop

Moritz Beller posted:

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
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top