By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

(default) constructor/object initializing problem

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi!

"Moritz Beller" <mo*****************@t-online.de> wrote in message
news:cg*************@news.t-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 -
Jul 22 '05 #2

P: n/a
On Mon, 30 Aug 2004 12:23:18 GMT
"Risto Lankinen" <rl******@hotmail.com> 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
Jul 22 '05 #3

P: n/a
Moritz Beller posted:
On Mon, 30 Aug 2004 12:23:18 GMT
"Risto Lankinen" <rl******@hotmail.com> 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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.