Article View: comp.lang.objective-c
Article #426Re: Can't see variable
From: Richard Frith-Macdonald
Date: Mon, 15 Feb 1999 00:00
Date: Mon, 15 Feb 1999 00:00
47 lines
2084 bytes
2084 bytes
george@gmsys.com (George Farris) wrote:
>I'm trying to learn Objective-C and am confused about the
>inheritance of instance variables. In the following code
>"Parent" has a variable called "name". "Child" is a subclass
>of "Parent". From reading information on Obj-C I understood that
>the "Child" would be able to see "name", it doesn't.
<snip>
>@implementation Child
>-init
>{
> printf("In Child name is ->%s\n", name);
> return self;
>}
>
>@end
The above code should read -
@implementation Child
-init
{
[super init];
printf("In Child name is ->%s\n", name);
return self;
}
@end
The initialiser for Child must call the initialiser for it's superclass
(in this case Parent).
Objective-C is unlike C++ in this respect -
In C++ the compiler calls constructors and desctructors automatically.
In Objective-C your code calls the 'alloc...', 'init...' and 'dealloc' methods
of the superclass itsself.
This means that in Objective-C, you have a little more work to do, but you gain
considerable flexibility in what you can do in the way of using different
construction/destruction techniques based on runtime decisions.
For the beginner like yourself, this will probably seem like a considerable annoyance -
'why should I have to remember to call [super init] in all my initialisers just to
gain flexibility I'll probably never use' -
Believe me - once you have written a few large projects in Objective-C and C++ you will
bless the extra flexibility of Objective-C.
Technical Details
Message-ID:
<36c7e548.0@nnrp1.news.uk.psi.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7a78io$vv$1@ve7frg.gmsys.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10e5ed,472bd2bca027a261 X-Google-Attributes: gid10e5ed,public X-Google-Thread: 1111a3,472bd2bca027a261 X-Google-Attributes: gid1111a3,public From: richard@brainstorm.co.uk (Richard Frith-Macdonald) Subject: Re: Can't see variable Date: 1999/02/15 Message-ID: <36c7e548.0@nnrp1.news.uk.psi.net> X-Deja-AN: 444528188 References: <7a78io$vv$1@ve7frg.gmsys.com> X-Trace: 15 Feb 1999 09:13:44 GMT, lin2.brainstorm.co.uk Newsgroups: comp.lang.objective-c,gnu.gnustep.discuss