I have a singleton class called playbackhelper to which I want to add a member that is a reference to a viewcontroller. But I don’t think I can make the member a reference because I want to be able to change which controller I’m linking to and I don’t think that’s possible with a reference.
This is my controller:
@interface InstrumentGridViewController : UIViewController {
}
-(void) someMethod;
@end
And this is my singleton class on which I want a member that is some kind of pointer to the controller.
class PlaybackHelper{
private:
// Singleton methods
PlaybackHelper();
PlaybackHelper(PlaybackHelper const&);
void operator=(PlaybackHelper const&);
public:
static PlaybackHelper &getInstance();
// ((((some InstrumentGridViewController member here))))
InstrumentGridViewController controllerMember;
}
So my question is: how can I link those with each other/how do I make a member that points to the controller and how should it be initialized?
EDIT: Basically I want to be able to call the controller methods from the singleton class, like this:
[PlaybackHelper::getInstance().controllerMember someMethod];
But I don’t know how to set my singleton up to make this possible. The way it is set up in the example right now controllerMember would be a copy of the controller object which is not what I want.
Debates about singleton use aside, if you are using ARC, you can easily use ObjC objects inside C++ classes, and memory management is taken care of.
If you want to make it an accessor…
If you just want to make it a public member…
ObjC objects are recognized to have non-trivial ctor/dtor, and the compiler will automatically take care of the ARC stuff.
LLVM + ARC + C++ makes it easy to mix ObjC objects with C++. In fact, you can do this without worrying a bit…
All the memory management is magically arranged by the compiler.