Question

I use Wrapper-Class using pimpl so I can use Objective-C methods while using C++.

Now in my ViewWrapper.hpp I have this:

class ViewWrapper
{
    public:
        void addSubview(ViewWrapper *view); //method exists in original View.h (Objective-C)
        ViewWrapper (void);
        ~ViewWrapper (void);
    private:
        struct Impl;
        Impl* impl;
};

In my ViewWrapper.mm:

//ViewWrapper.mm

#import "ViewWrapper.hpp"
#import "View.h" // original Objective-C header

struct ViewWrapper::Impl
{
    View* obj;
};

ViewWrapper::ViewWrapper() : impl(new Impl)
{
    impl->obj = [[View alloc] init];
}

void ViewWrapper::addSubview(ViewWrapper *view)
{
    [impl->obj addSubview:(View *) view]; // <- here is the error
}

Now in my main.cpp I use it like this:

// main.cpp

ViewWrapper *wrapper1 = new ViewWrapper();
ViewWrapper *wrapper2 = new ViewWrapper();
wrapper1->addSubview(wrapper2);

Now when I try to compile it, I get the error message

"Thread 1:EXC_BAD_ACCESS(code=2, address=0x105)" in my ViewWrapper.mm

I guess my problem is that this is not the correct way of casting ViewWrapper to View. Any other method works just fine (e.g. const char* to NSString and so on..)

Can anyone help me? If you need more info, I'll try to add it as good as I can.

Was it helpful?

Solution

I solved it myself. If anyone faces the same problem, I changed my addSubview-method like that:

[impl->obj addSubview:(UIView *) view->impl->obj];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top