Temporary and non-const member function

S

siddhu

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
 
V

Victor Bazarov

siddhu said:
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
 
R

red floyd

siddhu said:
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.
 
V

Victor Bazarov

red said:
[..] 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
 
S

siddhu

red said:
[..] 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

Thank you all for clearing my confusion.
 
R

red floyd

Victor said:
red said:
[..] 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.
 
J

James Kanze

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

James Kanze

[..] 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).
[/QUOTE]
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top