Re: Calling getter on const object
Re: Calling getter on const object
- Subject: Re: Calling getter on const object
- From: Jonny Taylor <email@hidden>
- Date: Mon, 8 Nov 2010 17:04:21 +0000
In this particular case I don't have much choice in the matter - the standard behaviour of blocks is to provide 'const' access to variables declared outside the block (and with good reason I think). In the example I gave, I want to access the property from within a block. I can work around it as shown in my example, or declare the function parameter (frame object pointer) with the __block prefix, but I feel that neither should be necessary.
I extended my question to the more general case of a getter on a const object because (maybe because I come from a c++ background?) I am in the habit of declaring object parameters as const if I do not expect them to change within a function, just to make it clear to myself what is or isn't happening, etc. I have situations where I don't see why I shouldn't declare a function parameter as 'const', because nothing within it is changing, but if I want to "read" *any* state of the object in objective c (via a getter) it seems that I cannot declare it const... which seems rather odd to me.
If you'll forgive my thinking in c++ terms, the equivalent in c++ would be to write something like:
int Frame::FrameNumber() const { return frameNumber; }
void SomeFunction(const Frame *frame)
{
int num = frame->FrameNumber(); // Would not be permitted in the objC equivalent, since 'frame' is const
}
// or one might alternatively write
void AnotherFunction(Frame *frame)
{
dispatch_async(analysisSerialQueue,
^{
// Do some processing
latestFrameNumberProcessed = frame->FrameNumber(); // Would not be permitted either in objC since frame becomes const within the block
});
}
On 8 Nov 2010, at 16:53, David Duncan wrote:
> My question is why do you need to have a 'const' pointer.
>
> On Nov 8, 2010, at 8:37 AM, Jonny Taylor wrote:
>
>> I'm afraid I'm not sure if I understand exactly what you're asking. I would like to be able access the "frame number" property (which is in fact stored as a variable within the class, and is fixed soon after instantiation) in spite of the fact that I only have a 'const' pointer to the object at the time I want to access it. Semantically I don't see why it should be unsafe to attempt to find out the value of that property.
>
> --
> David Duncan
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden