Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C inheritance; downcasting/copying from parent class to derived class

In my program I have a class, say ClassA. I'd like to create a derived class, say ClassB. My program has functions returning instances of ClassA and in certain cases I'd like to use these returns to create an instance of ClassB. Doing the naive downcasting doesn't lead to any compiler errors, but runtime errors creep in. This seems to be related to issues about casting pointers vs. objects -- at any rate, from what I've read, this was the wrong thing to do in this case.

I then attempted to mimic the copy-constructors of C++, but variables that were copied passed out of scope or were released, leading again to run-time errors.

I also considered using categories but this doesn't seem right for two reasons:

  1. First and foremost, I can't add new class members with categories and things will get a little convoluted if I can't do this.
  2. As near as I can tell the methods added through categories become available to all instances of the class. While this isn't, strictly speaking, a problem it doesn't sit well with me since certain aspects of the methods I want to add would break in a general setting; i.e. they are somehow specific to what I need to do with these types of objects.

I'm still learning Objective-C. I know there are some similar questions in the forum but the answers either led to categories and dead-ended or dead-ended without much help so I felt I should simply repost.

like image 825
Paul Avatar asked Dec 20 '25 22:12

Paul


1 Answers

In my program I have a class, say ClassA. I'd like to create a derived class, say ClassB.

@class MONClassA;

@interface MONClassB : MONClassA
{
    int anotherVariable;
}

//...
- (id)initWithMONClassA:(MONClassA *)classA anotherVariable:(int)anotherVar;

@end

My program has functions returning instances of ClassA and in certain cases I'd like to use these returns to create an instance of ClassB.

same way you'd do it in c++ (as it sounds like that's your background), promote the type with a new instance:

//...
MONClassA * a = SomeLibFunction();
MONClassB * b = [[MONClassB alloc] initWithMONClassA:a anotherVariable:???];
//...

Doing the naive downcasting doesn't lead to any compiler errors, but runtime errors creep in. This seems to be related to issues about casting pointers vs. objects -- at any rate, from what I've read, this was the wrong thing to do in this case.

correct - it is the wrong thing to do.

I then attempted to mimic the copy-constructors of C++, but variables that were copied passed out of scope or were released, leading again to run-time errors.

see the above implementation to see how to promote a type in objc. this of course assumes you'll also define an appropriate implementation for initialization (equivalent to your cpp constructor and copy constructor).

I also considered using categories but this doesn't seem right...

then your instinct is probably right. this language feature is misused quite often.

like image 168
justin Avatar answered Dec 23 '25 04:12

justin



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!