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

Temporary and non-const member function

P: n/a
Dear Experts,

I have a question.

I wrote the following code

class A
{
public:
void g(){}
};

A f()
{
A a;
return a;
}

int main(int argc, char *argv[])
{
f().g();

return 0;
}
Here f() is temporary on which g() is called which is a non-const
member function of A.
So my doubt is, as we know temporary is always const ,so how it can
call a non-const member function?
Code above is getting compiled.

Thanks in advance,
Siddhu

May 4 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
siddhu wrote:
I wrote the following code

class A
{
public:
void g(){}
};

A f()
{
A a;
return a;
}

int main(int argc, char *argv[])
{
f().g();

return 0;
}
Here f() is temporary on which g() is called which is a non-const
member function of A.
So my doubt is, as we know temporary is always const ,
Really? Who said that? Or, rather, where did you get that?

Anyway, you're incorrect. Temporaries are NOT const.
>so how it can
call a non-const member function?
Code above is getting compiled.
As it should.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 4 '07 #2

P: n/a
siddhu wrote:
Dear Experts,

I have a question.

I wrote the following code

class A
{
public:
void g(){}
};

A f()
{
A a;
return a;
}

int main(int argc, char *argv[])
{
f().g();

return 0;
}
Here f() is temporary on which g() is called which is a non-const
member function of A.
So my doubt is, as we know temporary is always const ,so how it can
call a non-const member function?
Code above is getting compiled.

As Victor said, temporaries are not const. However, i believe they can
only be bound to a non-const reference, hence your confusion.
May 4 '07 #3

P: n/a
red floyd wrote:
[..] i believe they
can only be bound to a non-const reference, hence your confusion.
Vice versa. Temporaries can only be bound to a reference to const
(a const reference).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 4 '07 #4

P: n/a
On May 4, 2:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
red floyd wrote:
[..] i believe they
can only be bound to a non-const reference, hence your confusion.

Vice versa. Temporaries can only be bound to a reference to const
(a const reference).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you all for clearing my confusion.

May 4 '07 #5

P: n/a
Victor Bazarov wrote:
red floyd wrote:
>[..] i believe they
can only be bound to a non-const reference, hence your confusion.

Vice versa. Temporaries can only be bound to a reference to const
(a const reference).
Typo. I meant to say const.
May 4 '07 #6

P: n/a
On May 4, 8:04 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Temporaries are NOT const.
Except when they are:-). Temporaries are rvalues, and rvalues
are funny beasts in C++. An rvalue of non class type doesn't
have const-ness (or volatile-ness); it's neither const nor
non-const. An rvalue of class type can be either const or
non-const; like everything else, it is non-const by default, but
if you qualify it as const, it becomes const.

We tend to think of them as non-const, because in many context,
there's no way to qualify them as const. In the actual example,
however, the reason the temporary is not const is because it
wasn't declared const. Had the function been declared:

A const f() ;

, the temporary would have been const.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 5 '07 #7

P: n/a
On May 4, 9:55 pm, siddhu <siddharth....@gmail.comwrote:
On May 4, 2:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
red floyd wrote:
[..] i believe they
can only be bound to a non-const reference, hence your confusion.
Vice versa. Temporaries can only be bound to a reference to const
(a const reference).
Thank you all for clearing my confusion.
Or adding to it, since the statements were incorrect. A
temporary of class type obeys exactly the same rules as a
non-temporary with respect to const-ness. You declared the
return type of the function non-const, so it is non-const. Had
you declared it const, it would have been const.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 5 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.