Does const go before or after CGFloat?

12,880

Solution 1

It can go either before or after. In the case of a pointer, what matters is whether the const ends up before or after the asterisk:

const int *a;    // pointer to const int -- can't change what a points at
int const *a;    // same

int *const a;    // const pointer to int -- can't change the pointer itself.
                 // Note: must be initialized, since it can't be assigned.

Solution 2

It doesn't matter (I've always used the former, but I guess it's a matter of style):

const CGFloat kPasscodeInputBoxWidth = 61.0;
CGFloat const kPasscodeInputBoxWidth = 61.0;

At least in the current rendition of CGFloat, it's just a typedef of double, so do as you would with a regular primitive datatype. For pointers, the placement of const will determine if it's the pointer or the value that is constant, so for that it does matter.

Share:
12,880
ma11hew28
Author by

ma11hew28

Updated on June 08, 2022

Comments

  • ma11hew28
    ma11hew28 almost 2 years

    Does it even matter? Const before or const after? I'm guessing that whether I put const before or after CGFloat it makes the value of CGFloat constant, but what about the pointer? Is this right for Objective-C:

    // Example.h
    
    extern CGFloat const kPasscodeInputBoxWidth;
    
    
    // Example.m
    
    CGFloat const kPasscodeInputBoxWidth = 61.0f;
    
  • pmg
    pmg about 13 years
    You can also have const int *const a;: a read-only pointer to read-only objects :)
  • ma11hew28
    ma11hew28 about 13 years
    CGFloat is actually a float, not a double.
  • Gustav Larsson
    Gustav Larsson about 13 years
    I think it varies between 32- and 64-bit systems. I run 64-bit and for me it's a double.
  • ma11hew28
    ma11hew28 about 13 years
    Interesting. I didn't know that. I was just going by line 89 of CGBase.h: typedef CGFLOAT_TYPE CGFloat;, which I got to by command-clicking on CGFloat in Xcode. But, how could the source code be different for you? Shouldn't it be the same on all machines? Hmmm... I thought I was running 64-bit too. How do wou know it's not a double?
  • Gustav Larsson
    Gustav Larsson about 13 years
    I'm not sure, I thought I had read that somewhere when you said it was float, but it might be double for everyone. It's not like double requires a 64-bit system. They could theoretically be different even though we have the same source code, through preprocessor defines.
  • Max MacLeod
    Max MacLeod about 11 years
    question relates to CGFloat not integer pointers. For integers, Apple guidelines state "You can use const to create an integer constant if the constant is unrelated to other constants; otherwise, use enumeration.". They only advocate const while using float. See developer.apple.com/library/mac/documentation/Cocoa/Conceptu‌​al/…
  • Jerry Coffin
    Jerry Coffin about 11 years
    @MaxMacLeod: The exact type (int, long, float, double, CGFloat, etc.) is pretty much irrelevant here.