C++ override function without subclassing

Discussion in 'Mac Programming' started by moonman239, Dec 29, 2015.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    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?
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    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++.
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    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.
     
  4. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    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.
     
  5. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    Is there not a way to force it? Like, cast to a void * then cast from that to your subclass?
     
  6. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #6
    C++ has dynamic_cast<>, is that what you mean?
     

Share This Page