I have a class (EAGLView
) which calls a member function of a C++
class without problems. Now, the problem is that I need to call in that C++
class a objective-C
function
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
which I cannot do in C++
syntax.
I could wrap this Objective-C
call to the same Objective-C
class which in the first place called the C++ class, but then I need to somehow call that method from C++
, and I cannot figure out how to do it.
I tried to give a pointer to EAGLView
object to the C++ member function and include the "EAGLView.h
" in my C++
class header but I got 3999 errors..
So.. how should I do this? An example would be nice.. I only found pure C
examples of doing this.
Step 1
Create a objective c file(.m file) and it's corresponding header file.
// Header file (We call it "ObjCFunc.h")
// Corresponding Objective C file(We call it "ObjCFunc.m")
Step 2
Now we will implement a c++ function to call the objective c function that we just created! So for that we will define a .mm file and its corresponding header file(".mm" file is to be used here because we will be able to use both Objective C and C++ coding in the file)
//Header file(We call it "ObjCCall.h")
//Corresponding Objective C++ file(We call it "ObjCCall.mm")
Step 3
Calling the c++ function(which actually calls the objective c method)
//Final call
Hope this works!
You can mix C++ in with Objectiv-C (Objective C++). Write a C++ method in your Objective C++ class that simply calls
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
and call it from your C++.I haven't tried it before my self, but give it a shot, and share the results with us.
The easiest solution is to simply tell Xcode to compile everything as Objective C++.
Set your project or target settings for Compile Sources As to Objective C++ and recompile.
Then you can use C++ or Objective C everywhere, for example:
This has the same affect as renaming all your source files from .cpp or .m to .mm.
There are two minor downsides to this: clang cannot analyse C++ source code; some relatively weird C code does not compile under C++.