Error in learning Cocoa book?
Error in learning Cocoa book?
- Subject: Error in learning Cocoa book?
- From: Ricky Sharp <email@hidden>
- Date: Sat, 13 Oct 2001 10:45:54 -0500
All,
In Chapter 9, page 162, readers are instructed to add an instance variable
to the Expenses class:
NSMutableArray* expenses;
and then an appropriate accessor. The book has the method declaration as:
- (NSMutableArray*) expenses;
but the implementation as:
- (NSArray*) expenses
{
return expenses;
}
Now, this does compile, link and run, but are there any side effects of
this? If anything, I would have thought that the declaration would also
have a return type of NSArray*. Or, both declaration and implementation
would have a return type of NSMutableArray*.
Let's assume that callers may or may not need mutable forms of the array
and the return type was made NSArray* in both cases.
if I then have...
NSArray* arrayOne = [anInstanceOfExpenses expenses];
NSMutableArray* arrayTwo = [anInstanceOfExpenses expenses];
What happens here? Similar question to if the Expenses class has the
return type of NSMutableArray...what happens?
I guess I'm seeing something similar to what you do in C++. e.g.
class Foo
{
private:
char* something;
public:
const char* operator * ( void ) { return something; }
char* operator * ( void ) { return something; }
}
Which allows callers to get acquire a const (read-only) or non-const
(read-write) pointer when using operator *.
TIA,
------------------------------------------------------------
Ricky A. Sharp Instant Interactive(tm)
Founder & President <
http://www.instantinteractive.com>
<
mailto:email@hidden>
------------------------------------------------------------