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

Regarding Temporary objects and const-ness !

P: n/a
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?
Nov 6 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
co**************@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
Nov 6 '08 #2

P: n/a
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!!
Nov 6 '08 #3

P: n/a
co**************@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-lit...t.html#faq-5.8
Please review the question!!
I've already answered your question assuming the above ctor.

Schobi
Nov 6 '08 #4

P: n/a
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_in_English
Nov 6 '08 #5

P: n/a
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.

Nov 6 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.