16

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;
Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 1
    what about what pointer? – pmg May 03 '11 at 20:59
  • I mean, the memory address of the value of the `CGFloat`. Does that stay constant? – ma11hew28 May 04 '11 at 13:20
  • 1
    `kPasscodeInputBoxWidth` identifies an object (of unqualified type `CGFloat`). That object (as all other objects) `"... exists, has a constant address, and retains its last-stored value throughout its lifetime ..."` (See 6.2.4/2 in the [C99 Standard](http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf)), so, by definition, the address of the object is constant. – pmg May 04 '11 at 13:42

2 Answers2

23

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.
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 4
    You can also have `const int *const a;`: a read-only pointer to read-only objects :) – pmg May 03 '11 at 21:00
  • 2
    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 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.pdf – Max MacLeod Feb 21 '13 at 11:35
  • @MaxMacLeod: The exact type (int, long, float, double, CGFloat, etc.) is pretty much irrelevant here. – Jerry Coffin Feb 21 '13 at 14:07
6

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.

Gustav Larsson
  • 8,199
  • 3
  • 31
  • 51
  • `CGFloat` is actually a `float`, not a `double`. – ma11hew28 May 04 '11 at 13:19
  • 1
    I think it varies between 32- and 64-bit systems. I run 64-bit and for me it's a double. – Gustav Larsson May 04 '11 at 13:40
  • 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`? – ma11hew28 May 07 '11 at 03:45
  • 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. – Gustav Larsson May 07 '11 at 05:36