iDevGames Forums
Accessing c++ class from Obj-C .h file - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Tools & Technology (/forum-10.html)
+--- Thread: Accessing c++ class from Obj-C .h file (/thread-1069.html)

Accessing c++ class from Obj-C .h file - dsedov - Jun 30, 2009 09:59 PM

Hello. I am new here, and just started learning Obj-C. I do have a lot of experience programming in C++ and C# though. Now the problem I am facing is that I want to write most of my classes in C++ rather Obj-C and use them inside Obj-C. I have found alot of information on ths matter and in most cases one just includes "cppclass.h" inside a ".mm" file and and just uses the class. This works just fine as ".mm" compiles nicely as Obj-C++, however when one has class definition and class body in seperate files as in ".h" and ".mm" it becomes harder to use C++ class, as inclusion of "cppclass.h" inside "objc.h" results in the following error:

"error:expected '=', ',',';','asm' or '__attribute__' before 'GoaEngine'

now including "cppclass.h" streight inside ".mm" file compiles just fine. So how do I use C++ class inside a complex Obj-C++ class that is broken into ".h" and ".mm"?

Here is a code snippets just in case if I've done something stupid:

class CppClass
        void setId(int newId);
        int getId();
        int myId;

#endif // FOO_H_INCLUDED

#include "cppclass.h"
void CppClass::setId(int newId)
    myId = newId;
int CppClass::getId()
    return myId;

Now I have EAGLView.h and EAGLView.m (which I renamed to .mm") created by OpenGL ES Template for iPhone SDK. Just writing #include "cppclass.h" inside EAGLView.h brings up an error. =(

So how do I use C++ class inside Obj-C ?

Accessing c++ class from Obj-C .h file - Oddity007 - Jul 1, 2009 05:50 AM

From what I know of, .mm is the only option. Problem is that with c++ everything gets compiled as c++.

You could try:
//c++ File
class MyClass{int x;} A;
//c header
typedef struct {int x;}MyClassAlias;
extern MyClassAlias A;
Of course this means no methods.

Accessing c++ class from Obj-C .h file - DoG - Jul 1, 2009 01:53 PM

All ObjC modules that use C++ headers need to be compiled as ObjC++, aka all modules that include a C++ header must be .mm files.

Accessing c++ class from Obj-C .h file - dsedov - Jul 1, 2009 11:07 PM

Yes, I understand about .mm. The problem I'm having is that if I include "cppclass.h" in everything works, and I can create instances of that class. Now if I include it in EAGLView.h - it does not compile =(