Senseful
Senseful

Reputation: 91701

Struct inheritance in Objective-C?

Is there anything similar to struct inheritance in Objective-C?

For example, C++ allows me to do something like:

struct animal {
    int weight;
    string name;
}

struct dog : animal {
    string breed;
}

struct cat : animal {
    bool playsWithLaserPointer;
}

Is there any way to do the same thing in Objective-C without resorting to Objective-C++?

Upvotes: 3

Views: 1589

Answers (5)

Sungwook Kim
Sungwook Kim

Reputation: 354

Struct itself in objc or swift is being treated as value type rather than reference type that is why struct in objc or swift does not have inheritance. It is more natural to treat them value type having no inheritance in terms of object oriented paradigm. In other words, objc or swift is designed to be purely object oriented language, not evolved being to be object oriented with many limits that is C++.

Upvotes: 0

Cy-4AH
Cy-4AH

Reputation: 4585

struct come in objective-c from C, that doesn't have inheritance. But no one forbid you use objective-C++, which have struct inheritance, just use .mm-files instead .m.

Upvotes: -1

Thorsten
Thorsten

Reputation: 3122

structs in structs work:

struct animal {
    int weight;
    char *name;
};

struct dog {
    struct animal base;
    char *breed;
};

struct cat {
    struct animal base;
    bool playsWithLaserPointer;
};

Apple uses this in CGRect with CGPoint and CGSize

Upvotes: 4

eswick
eswick

Reputation: 519

Unfortunately, there is no struct inheritance in Objective-C. However, there are several alternatives, which I personally find preferable to struct inheritance.

What I would recommend is creating a class for the storage of your variables. Classes have inheritance, and their properties are accessed in the same syntactic way you access variables in structs. (with the exception of struct pointers, I suppose)

@interface Animal : NSObject
@property int weight;
@property NSString *name;
@end

@interface Dog : Animal
@property NSString *breed;
@end

@interface Cat : Animal
@property BOOL playsWithLaserPointer
@end

/* Implementations */
@implementation Animal
@synthesize weight = _weight
...

Then, the properties are accessed just like structs

Cat *myCat = [cat new];
myCat.name = @"Maow";
NSLog(@"My cat, %@, %@ with laser pointers.", myCat.name, (myCat.playsWithLaserPointer ? @"plays" : @"doesn't play" ));
[myCat release];

The upside to this method is that getters and setters can be overloaded to act differently (default values, etc). Objects can also be stored in NSArrays and NSDictionaries, and if you implement it, they can be serialized for easy storage in a plist for later use. Also, allocating new objects is much simpler than allocating space for a new struct.

Upvotes: 2

rmaddy
rmaddy

Reputation: 318824

You could make these into trivial classes with public ivars. That would be the closest you will get. But you may as well make them real classes. Use properties to encapsulate the ivars. Add an init method as well as isEqual: and hash methods, etc. Then you can get helpful memory management as well as all of the helpful methods with just trivial overhead (memory-wise) over plain old structs.

Upvotes: 1

Related Questions