C
coolguyaroundyou
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?
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?