Re: ivar name clashes with gcc3
Re: ivar name clashes with gcc3
- Subject: Re: ivar name clashes with gcc3
- From: Ricky Sharp <email@hidden>
- Date: Wed, 8 Feb 2006 16:14:56 -0600
On Feb 8, 2006, at 8:05 AM, Camillo Lugaresi wrote:
I have the feeling that this might turn out to be a dumb question,
but...
I have a program that is usually built on 10.4 using gcc4. One
class has an ivar named "outline", which is also the name of an
enum value defined in MacTypes.h. Now I need to build this program
on Panther using gcc3, and it seems to have issues with that
variable: namely, unless it is prefixed with "self->", "outline" is
always taken as a reference to the enumerated constant instead of
to the variable. This causes all sorts of errors at compile time
(not an lvalue, wrong type etc) and at runtime (instead of the
value of the variable, 8 is always used).
The problem could be worked around by explicitly referencing the
variable using "self->outline", but it would make the code ugly,
and the bug is likely to reappear in the future. Is there some
compiler setting that I can use to get the same behavior as gcc4
with gcc3?
Not sure of any compiler setting for ivars, but what I currently do
is suffix all my ivars with _II (My company initials). I also modify
all methods to include _II.
For example:
int value_II;
- (int)computeSum_II:(int)anAugend addend:(int)anAddend;
I did this to primarily avoid name collisions with Apple.
Around June 2005, one of my controls had a currentValue ivar. So,
the getter in the control class was:
- (int)currentValue
{
return [[self cell] currentValue];
}
But, [self cell] returns an id. This wasn't a big deal prior to 10.4
since there was no Cocoa API named currentValue.
But in 10.4, Apple introduced a currentValue API as part of
NSAnimation. To make matters worse, I did not have -Wstrict-selector-
match specified in my 'Other C Flags'. So, the compiler
automatically generated code to call currentValue against the first
recipient it found (which was NSAnimation and not my class). Very
strange things at runtime :)
One potential fix was to cast the return value of [self cell]. But I
opted against that.
Currently, I do have that compiler setting just as a failsafe. And I
chose to rename my stuff to avoid all possible collisions.
Hmm...maybe there's a separate setting for "strictness" with ivars?
Finally, other reasons why I went "nuts" in renaming everything:
* Wanted to guarantee uniqueness of names used for custom bindings.
My controls often use inherited bindings, but I sometimes need to
provide my own implementation. Having _II allows me uniqueness and
also it's easy to see at-a-glance what are my bindings vs. those
provided (as seen in the bindings inspector in IB).
* There's a very small chance that some of my code may be released
for the consumption of other developers. I wanted to ensure no
collisions would exist with their code.
What I've effectively done is guarantee all my code lives within its
own "namespace".
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden