Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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++?

like image 565
Senseful Avatar asked Dec 15 '25 01:12

Senseful


1 Answers

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

like image 114
Thorsten Avatar answered Dec 16 '25 22:12

Thorsten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!