Skip to main content
gamedev.net
🔒 Locked

const problem...

Started by mrmrcoleman Jan 20, 2005 at 12:05 PM 3 replies 450+ views
Original Post
mrmrcoleman
mrmrcoleman
I am having trouble with a method which accepts a reference to a const RECT. It looks like this:

void SetPrintRect(const RECT& RPrint) { DoSetPrintRect(RPrint); }

I am trying to use this function but I am not sure how to do it! I tried this but it doesn't work because the RECT is not const.

RECT CustomRect.
CustomRect.top = 10;
etc etc

SetPrintRect(&CustomRect);

Then I tried this:

const RECT CustomRect;
// assignments here
SetPrintRect(&CustomRect);

But obviously this won't work because I can't assign to a const RECT, so what do I do? I know this obviously seems like a stupid question, but I don't really use const at all in my programming (before you start to flame, just stop). Thanks for any help. Mark Coleman
BigBadBob
BigBadBob
your problem is the function accepts a reference, you are passing a pointer value (the address of the RECT).

when a function accepts a reference, the calling code is no different than pass by value.

RECT rect;
// stuff here
SetPrintRect(rect);
ontheheap
ontheheap
AFAIK, you don't have to pass a const object to a function expecting a const object. It's just a "promise" saying "I won't change your data." It's safe to pass a non-const as a const argument because it's already safe to change the data. In this case you're just saying you won't. The other way around, however (passing a const object to a function requiring a non-const object won't work). I get the feeling that this isn't what you're looking for though...
ajas95
ajas95
You don't use CONST ?!?! You Idiot! Of all the programming practices.....
Ok, I'm just kidding.

The problem is probably that DoSetPrintRect() doesn't take a const parameter. 'const' attached to an argument to the function just promises that the function won't change it, it's not a requirement on the input passed to it. The side-effect of that is that the function taking the const argument can't then pass it to some other function as a non-const argument (in this case DoSetPrintRect).

Something like:

void DoSetPrintRect(const RECT& RPrint) { /* blahbitty blah blah. */ }void SetPrintRect(const RECT& RPrint) { DoSetPrintRect(RPrint); }RECT CustomRect;CustomRect.top = 10;SetPrintRect(CustomRect); // note you don't need the ampersand here.

should work.

mrmrcoleman
mrmrcoleman
Thanks everyone for your help, ho hum! Should have consulted the good old C++ Programming Language but I have been in and out of it all day and I just wanted AN EASY ANSWER, which I got!!

Thanks again, and thanks for the lack of flames!

Kind regards.

Mark Coleman

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.