[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: operator overloading
From: |
Sheldon Gill |
Subject: |
Re: operator overloading |
Date: |
Tue, 07 Mar 2006 09:05:56 +0800 |
User-agent: |
Thunderbird 1.5 (Windows/20051201) |
Marko Riedel wrote:
Hi folks,
here is a question for you GNUstep wizards out there. Can anyone suggest
how best to simulate operator overloading, which is missing from Objective
C? I would like to overload operators such as "add" or "multiply." Can I
ask GCC to output its parse tree and then try to add overloading manually?
I'm not sure what exactly you are trying to achieve or why you want this.
Anyway, Objective-C is O-O rather than functional so operator overloading isn't
needed. Just add the appropriate methods to classes so you might have:
-[EgMatrix add: (EgMatrix *)anotherMatrix];
-[EgComplex add: (EgComplex *)anotherComplex];
If you're thinking C++ operator overloading then you're looking for a method
lookup which considers the type/class of the argument... so...
If you wish to "overload" the add method then you need to change the signatures
slightly:
-[EgMatrix add: (id *)aNumericObject];
-[EgMatrix addMatrix: (EgMatrix *)anotherMatrix];
-[EgMatrix addComplex: (EgComplex *)anotherMatrix];
-[EgComplex add: (id *)aNumericObject];
-[EgComplex addMatrix: (EgMatrix *)anotherMatrix];
-[EgComplex addComplex: (EgComplex *)anotherComplex];
then add introspection like this
- (id)add: (id)aNumericObject
{
if (aNumericObject_isa_EgComplex)
return [self addComplex: aNumericObject]
if (aNumericObject_isa_EgMatrix)
return [self addMatrix: aNumericObject]
}
Considering the similarity of the code, rather than try to get GCC's parse tree
it'd be *much* easier and equally effective to write a template compiler. It
really only needs to be a rather dumb pre-processor for this level of
complexity. You may even get away with using the standard C pre-processor.
> BTW is there a tutorial on how to load classes at runtime (sorry if this
> is a very basic question).
Look at the documentation for NSBundle.
Regards,
Sheldon