Re: why Obj-C
Re: why Obj-C
- Subject: Re: why Obj-C
- From: Brian Stern <email@hidden>
- Date: Tue, 9 Apr 2002 17:09:06 -0400
At 4:03 PM +0200 4/9/02, Thomas Lachand-Robert wrote:
>
Le mardi 9 avril 2002, ` 02:36 , Marcel Weiher a icrit :
>
> On Tuesday, April 9, 2002, at 12:32 Uhr, Ondra Cada wrote:
>
>> On Tuesday, April 9, 2002, at 03:28 , John C. Randolph wrote:
>
>>> foo = [[MyClass alloc] initWithSomeOtherObject:[OtherClass randomObject]
>
>>> ];
>
>>
>
>> foo=MyClass->alloc()->initWithSomeOtherObject(OtherClass->randomObject()
>
>> )
>
>> ;
>
>
>
Actually this doesn't really work and is certainly bad C++ style.
>
If you write
>
foo = MyClass->alloc()->initWithSomeOtherObject(OtherClass()->randomObject(
>
))
>
the programm will crash if any of the function returns a null pointer.
>
>
For this reason, a good C++ programmer will do something like:
>
MyClass *p = MyClass->alloc();
>
if (p) {
>
Other *q = OtherClass();
>
if (q) {
>
p-> initWithSomeOtherObject( q->randomObject() );
>
//continue...
>
}
>
}
This is simply not true. Operator new throws an exception on failure. A
constructor for a class written by a good C++ programmer will also throw an
exception if the object cannot be constructed. As a result it is
impossible for a constructor to return a NULL pointer.
Further, the syntax of constructors, copy constructors, and assignment
operators seems to be neater and terser than the Objective-C equivalents.
The above example would more naturally be written:
MyClass *p = new MyClass(randomObject);
or
MyClass p = randomObject; // allocate p on the stack
--
Brian Stern
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Re: why Obj-C (From: Thomas Lachand-Robert <email@hidden>) |