Can a property of a class be the class itself?

131 views Asked by At

Can a class have a property and the class of the property is the class itself?

@interface testClass:NSObject
@property (nonatomic, strong) testClass *testProperty;
@end

@implementation testClass
@end
1

There are 1 answers

1
Anoop Vaidya On BEST ANSWER

Yes it can have, this is called self-referencing class.

Why it is not a compiler issue ?

Because you create a pointer to the object of same type, not the object.

This is frequently seen in C / C++ data structures like Linked List.