Re: why Obj-C
Re: why Obj-C
- Subject: Re: why Obj-C
- From: Dan Crevier <email@hidden>
- Date: Thu, 04 Apr 2002 21:58:59 -0800
On 4/4/2002 9:36 PM, "Andy Lee" <email@hidden> wrote:
>
At 9:02 PM -0800 4/4/02, Dan Crevier wrote:
>
> On 4/4/2002 8:46 PM, "Andy Lee" <email@hidden> wrote:
>
>
>
>> * "self" is really "self", by which I mean the method that gets
>
>> invoked for an object depends on the object's actual class at
>
>> runtime, unlike C++ where the method invoked depends on the
>
>> variable's declared type.
>
>
>
> Maybe I'm just missing what you are saying here, but this is the basic
>
> meaning of polymorphism, and is the way C++ works too. For example:
>
>
>
> class A
>
> {
>
> public:
>
> virtual void foo();
>
> void bar() { this->foo(); }
>
> };
>
>
>
> class B
>
> {
>
> public:
>
> virtual void foo();
>
> };
>
>
>
> void f()
>
> {
>
> B *b = new b;
>
> A *bAsA = b;
>
>
>
> bAsA->foo(); // calls B::foo()
>
> bAsA->bar(); // calls B::foo()
>
> }
>
>
You're right in this example, but as I recall, if bAsA->foo() is
>
invoked in A's constructor, it will call A::foo(), on the grounds
>
that it's not safe to use B::foo() before the object's B-ness has
>
been initialized. (No jokes, please, about the object's "A-ness.")
>
This is consistent with C++'s highly protective design and
>
philosophy, but different from Java and Objective-C.
>
>
In Objective-C, [bAsA foo] would *always* call [B -foo] no matter
>
where it's called. Likewise in Java, even in a constructor, and for
>
that matter, in Smalltalk as well.
You are right, the constructor is an exception. A common way around this is
to call a function on new objects after created. This is just like the
Objective C init function. The exception for constructors is pretty
confusing for people new to C++, but makes perfect sense when you think
about it. Objective C avoids it by not having constructors.
Dan
_______________________________________________
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.