[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Regarding Temporary objects and const-ness !

coolguyaroundyou

11/6/2008 9:23:00 AM

Consider the following codes:

class abc
{
int i;

public:
abc() : i(0) {}
void func() { .....some code....}
};

void func2(const abc& Obj)
{
.....some code...
}


int main()
{
func2(12);...................//CALL 1

const abc obj;
obj.func();..................// CALL 2

abc().func();...............// CALL 3

return 0;
}

CALL 1 takes place fine by implicit conversion of 5 to an abc object.
But, if the func2() paramater is kept non-const, then it gives
error !! WHY??

CALL2 doesn't take place because we are calling a non-const function
on a const object. That's understood.

If you say that the reason for CALL 1 not taking place in case of non-
const function parameter is that the temporary object created is a
const, then what is the reason for CALL 3 taking place successfully??

I'm really confused regarding this problem.


Are temporary objects const??
If not, then why do we need the prototype of func2 as reference to a
const?
6 Answers

alasham.said

11/6/2008 11:47:00 AM

0

On Nov 6, 10:22 am, coolguyaround...@gmail.com wrote:
> Consider the following codes:
>
> class abc
> {
> int i;
>
> public:
> abc() : i(0) {}
> void  func() { .....some code....}
>
> };
>
> void func2(const abc& Obj)
> {
> ....some code...
>
> }
>
> int main()
> {
> func2(12);...................//CALL 1
>
> const abc obj;
> obj.func();..................// CALL 2
>
> abc().func();...............// CALL 3
>
> return 0;
>
> }
>
> CALL 1 takes place fine by implicit conversion of 5 to an abc object.
> But, if the func2() parameter is kept non-const, then it gives
> error !! WHY??

Should not compile. You can not initialise const abc& with the
literal 12. Nothing to do with constant correctness.
In order to do that, you should provide an appropriate conversion
constructor.



>
> CALL2 doesn't take place because we are calling a non-const function
> on a const object. That's understood.
>
> If you say that the reason for CALL 1 not taking place in case of non-
> const function parameter is that the temporary object created is a
> const, then what is the reason for CALL 3 taking place successfully??
>
> I'm really confused regarding this problem.
>
> Are temporary objects const??
> If not, then why do we need the prototype of func2 as reference to a
> const?

You do not _need_ the parameter to be a constant reference. You make
it a constant reference to enforce the fact the the value being passed
to the function parameter will not be changed by the function.

This part of the FAQ may be helpful.
http://www.parashift.com/c++-faq-lite/const-correc...

Regards

Hendrik Schober

11/6/2008 1:37:00 PM

0

coolguyaroundyou@gmail.com wrote:
> Consider the following codes:
>
> class abc
> {
> int i;
>
> public:
> abc() : i(0) {}
> void func() { .....some code....}
> };
>
> void func2(const abc& Obj)
> {
> .....some code...
> }
>
>
> int main()
> {
> func2(12);...................//CALL 1
>
> const abc obj;
> obj.func();..................// CALL 2
>
> abc().func();...............// CALL 3
>
> return 0;
> }
>
> CALL 1 takes place fine by implicit conversion of 5 to an abc object.
> But, if the func2() paramater is kept non-const, then it gives
> error !! WHY??

In the code you supplied CALL 1 won't compile. Please make
sure you provide code we can paste and compile to observe
what you claim to see. (That's an FAQ. Please take the time
to read and understand the relevant portions of the FAQ
before you ask here.)

Assuming you have 'abc::abc(int)' forgotten to paste, but
have it in your real code: A ctor taking one argument (or
taking more, but the other's are optional) which isn't
marked as 'explicit' is an user-defined implicit conversion
operator. If, where an 'abc' is needed, you supply something
else, and if it takes only one user-defined conversion, the
compiler will employ such ctors to perform the conversion.
Thus, 'func2(12)' is converted to 'func1(abc(12))' and this
is fine, since the compiler can bind temporaries to const
references. If, however, 'func2()' takes a non-const reference
it won't compile, because it isn't allowed to bind temporaries
to non-const references.

> CALL2 doesn't take place because we are calling a non-const function
> on a const object. That's understood.
>
> If you say that the reason for CALL 1 not taking place in case of non-
> const function parameter is that the temporary object created is a
> const, then what is the reason for CALL 3 taking place successfully??

The temporary isn't 'const'. It's an rvalue. You can call member
functions, even non-const ones, on rvalues of user-defined types
(even though you cannot modify rvalues of built-in types).

> I'm really confused regarding this problem.
>
>
> Are temporary objects const??
> If not, then why do we need the prototype of func2 as reference to a
> const?

HTH,

Schobi

coolguyaroundyou

11/6/2008 3:45:00 PM

0

I'M EXTREMELY SORRY EVERYONE

The class also defines a parametrized constructor :

class abc
{
int i;
public:
abc() : i(0) {}
abc(int x) : i(x) {}
void func() { .....some code....}
};

Please review the question!!

Hendrik Schober

11/6/2008 3:54:00 PM

0

coolguyaroundyou@gmail.com wrote:
> I'M EXTREMELY SORRY EVERYONE
>
> The class also defines a parametrized constructor :
>
> class abc
> {
> int i;
> public:
> abc() : i(0) {}
> abc(int x) : i(x) {}
> void func() { .....some code....}
> };

This still doesn't compile.
http://www.parashift.com/c++-faq-lite/how-to-post.ht...

> Please review the question!!

I've already answered your question assuming the above ctor.

Schobi

Obnoxious User

11/6/2008 5:21:00 PM

0

On Thu, 06 Nov 2008 07:45:06 -0800, coolguyaroundyou wrote:

> I'M EXTREMELY SORRY EVERYONE
>
> The class also defines a parametrized constructor :
>
> class abc
> {
> int i;
> public:
> abc() : i(0) {}
> abc(int x) : i(x) {}
> void func() { .....some code....}
> };
>
> Please review the question!!

Review what question?

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

Salt_Peter

11/6/2008 6:35:00 PM

0

On Nov 6, 4:22 am, coolguyaround...@gmail.com wrote:
> Consider the following codes:
>
> class abc
> {
> int i;
>
> public:
> abc() : i(0) {}
> void func() { .....some code....}
>
> };
>
> void func2(const abc& Obj)
> {
> ....some code...
>
> }
>
> int main()
> {
> func2(12);...................//CALL 1
>
> const abc obj;
> obj.func();..................// CALL 2
>
> abc().func();...............// CALL 3
>
> return 0;
>
> }
>
> CALL 1 takes place fine by implicit conversion of 5 to an abc object.
> But, if the func2() paramater is kept non-const, then it gives
> error !! WHY??
>
> CALL2 doesn't take place because we are calling a non-const function
> on a const object. That's understood.
>
> If you say that the reason for CALL 1 not taking place in case of non-
> const function parameter is that the temporary object created is a
> const, then what is the reason for CALL 3 taking place successfully??
>
> I'm really confused regarding this problem.
>
> Are temporary objects const??
> If not, then why do we need the prototype of func2 as reference to a
> const?

No, temporaries are rvalues. They are not constant and hence the
problem.
What prevents member function func(...) (in CALL 3) to modify the
temp?

Assuming a parametized constructor, fun2 needs a reference to constant
because the lifetime of the temporary needs to be extended.

func2(abc(12));

might fail if it were allowed to compile as is.

Look at it this way:

abc& r = abc();
r.func();

what guarantee do you have that the temporary isn't destroyed before
the second statement?
Well, the language does offer a guarentee.

const abc& r_ = abc();

extends the lifetime of the temporary until the reference_to_const
dies.