• Create Account

### #ActualCornstalks

Posted 04 April 2013 - 08:34 AM

Alright, now that I'm on a computer instead of a phone, I can properly respond to The King2's post...

// ...
int main()
{
testConstness(tempCopy); //compiles, but storage of reference/pointer will be invalid as soon as the main() function exits -> imagine this in other //function


Nope, that won't compile. For two reasons. One, you're not calling the function, you're passing the address of the function. And two, even if you did call the function, it would be binding a non-const reference to a temporary, which is illegal.

    testConstness(constCopy); //won't compile


No, it won't, but that's because the constCopy function isn't declared right (it's missing parentheses at the end), and also because you're not calling it here (again, you're passing the address of the function). Then, of course, you have the whole "binding a non-const reference to a temporary" issue. But yes, you are right in that you can't use a non-const reference to refer to a const object.

    foo nonconst;
testConstness(nonconst); //compiles
const foo isconst;
testConstness(isconst); //won't compile
}


These two are actually right.

### #1Cornstalks

Posted 04 April 2013 - 08:33 AM

Alright, now that I'm on a computer instead of a phone, I can properly respond to The King2's post...

// ...
int main()
{
testConstness(tempCopy); //compiles, but storage of reference/pointer will be invalid as soon as the main() function exits -> imagine this in other //function


Nope, that won't compile. For two reasons. One, you're not calling the function, you're passing the address of the function. And two, even if you did call the function, it would be binding a non-const reference to a temporary, which is illegal.

    testConstness(constCopy); //won't compile


No, it won't, but that's because the constCopy function isn't declared right (it's missing parentheses at the end), and also because you're not calling it here (again, you're passing the address of the function). Then, of course, you have the whole "binding a non-const reference to a temporary" issue. But yes, you are right in that you can't use a non-const reference to refer to a const object.

    foo nonconst;
testConstness(nonconst); //compiles
const foo isconst;
testConstness(isconst); //won't compile
}


These two are actually right.

Please, when posting code that's supposed to prove/illustrate something, make sure it works/is right ;)

PARTNERS