Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
In Objective-C, we have this thing called "method swizzling", where we can basically redefine a method without creating a subclass. This ability is most helpful when we are dealing with an object that doesn't let us define a subclass for a given object. Suppose, for example, that we have two classes that are provided to us by Apple. Calling a method of class A gives us an object of class B. Class B has a method called "method", and we want something more/different to happen when "method" is called. In that case, we can "swizzle" the method.

I can see from my C++ programing book that there is no way to do such a thing in C++; any overriding is to happen from a derived class. Makes me wonder, though: Is there a workaround?
 

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
You're hitting on one of the fundamental differences between Objective-C and C++. As stated, there's no general way in C++ to do what you want to do. It might be possible to set up C++ classes to do what you want, but it would take some design work to get there. In other words, it would have to be designed in from the start. The decision about which non-virtual object method is going to get called is done at compile time in C++, not run-time like in Objective-C. Unless the method is specifically declared as virtual in C++.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,061
I don't deal with C++ much and I've definitely never tried this, but maybe you could define a subclass of B with the override you want, call the method of A that returns B, then cast that instance of B to be an instance of a subclass of B. So long as the subclass doesn't add any additional member variables, I don't think there's any actual difference between how the two are stored in memory. The sole purpose of the cast is to change which method, which is not stored within the instance in memory, will be pointed at at compile time.

Of course, this requires the C++ compiler to allow you to perform a down-cast like that. I don't know if it does.
 

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
I don't deal with C++ much and I've definitely never tried this, but maybe you could define a subclass of B with the override you want, call the method of A that returns B, then cast that instance of B to be an instance of a subclass of B. So long as the subclass doesn't add any additional member variables, I don't think there's any actual difference between how the two are stored in memory. The sole purpose of the cast is to change which method, which is not stored within the instance in memory, will be pointed at at compile time.

Of course, this requires the C++ compiler to allow you to perform a down-cast like that. I don't know if it does.

I think that kind of down-cast is not supported by the ISO standard. I think if you do that, you'll end up with a complaint because the compiler knows the instance is not of type B. However, you can always create an instance of a subclass that holds the same information as that of the instance returned by the method. This workaround works in those cases where location doesn't matter.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,061
I think that kind of down-cast is not supported by the ISO standard. I think if you do that, you'll end up with a complaint because the compiler knows the instance is not of type B. However, you can always create an instance of a subclass that holds the same information as that of the instance returned by the method. This workaround works in those cases where location doesn't matter.

Is there not a way to force it? Like, cast to a void * then cast from that to your subclass?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.