Skip to content

Instantly share code, notes, and snippets.

@uliwitness
Created April 7, 2017 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uliwitness/95f167249c1658d3f1026b60eff409f5 to your computer and use it in GitHub Desktop.
Save uliwitness/95f167249c1658d3f1026b60eff409f5 to your computer and use it in GitHub Desktop.
Proof-of-concept for how one could implement Objective C's target/action paradigm in a static fashion in C++. Basically, the GeneratedWindow class would be what your UI builder (like Interface Builder) generates. Button would be in your application framework. ViewController and the main() function would be how you'd then use them.
//
// main.cpp
// CppTargetActionOutlets
//
// Created by Uli Kusterer on 07.04.17.
// Copyright © 2017 Uli Kusterer. All rights reserved.
//
#include <iostream>
#include <functional>
using namespace std;
using namespace std::placeholders;
class Button
{
public:
void DoPush() { if( action ) action(this); }
std::function<void(Button*)> action;
};
class ViewController
{
public:
void DoOKButton( Button* inButton ) { cout << "Button clicked." << endl; }
};
class GeneratedWindow
{
public:
GeneratedWindow( ViewController* inOwner ) { mButton.action = std::bind(&ViewController::DoOKButton, inOwner, _1); }
Button mButton;
};
int main( int argc, const char * argv[] )
{
ViewController theController;
GeneratedWindow theWindow( &theController );
theWindow.mButton.DoPush();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment