From http://www.cnblogs.com/biosli/archive/2011/04/30/Mixing_ObjectiveC_and_Cplusplus_in_iPhone_Development.html
Let's talk about the problem first,ArticleThe title is actually not good. In iOS development, the libraries created by Apple are basically written in objective-C, therefore, Here c ++ refers to objective-C ++.
First, the most important thing is notCodeIt is a compiler option. Before mixed compilation, you must change the compiler's compile sources as option to Objective C ++.
The default option is according to file type. If you use this option, each class that is not in the Cross columns is OK. Once the two languages call each other in a file, an error is returned, in addition, the error message is very strange, such as: New cannot be found, delete cannot be found, and so on.
Since it is called, there must be some time to come. First, let's talk about how to call objective-C in C ++: (refer to this and I will make some modifications)
1. Function interfaces that encapsulate objective-C object functions into C
2. Write a corresponding C ++ class call
To put it simply, let's look at the example to see what's going on.
// MyObject-C-Interface.h
# Ifndef _ myobject_c_interface_h __
# Define _ Myobject_c_interface_h _ 1
IntMyobjectdosomethingwith (Void *Myobjectinstance,Void *Parameter );
# Endif
//Myobject. h
# Import"MyObject-C-Interface.h"
@ Interface myobject: nsobject
{
IntSomevar;
}
-(Int) Dosomethingwith :(Void *) Aparameter;
@ End
//Myobject. m
# Import"Myobject. h"
IntMyobjectdosomethingwith (Void *Self,Void *Aparameter)
{
Return[(ID) Self dosomethingwith: aparameter];
}
@ Implementation myobject
-(Int) Dosomethingwith :(Void *) Aparameter
{
//... Some code
Return 1;
}
@ End
//Mycppclass. h
# Ifndef _ mycppclass_h __
# Define_ Mycppclass_h __
ClassMycppclass
{
Public:
IntMycppclass: somemethod (Void *Objectivecobject,Void *Aparameter );
}
# Endif
//Mycppclass. cpp
# Include"Mycppclass. h"
# Include"MyObject-C-Interface.h"
IntMycppclass: somemethod (Void *Objectivecobject,Void *Aparameter)
{
ReturnMyobjectdosomethingwith (objectivecobject, aparameter );
}
As shown above, we first construct a weak type C function interface, and then implement this interface in the Objective C class. Finally, the weak types and interfaces are called in the C ++ class.
On the premise that the main framework of the Project is written in objective-C, the above call is not practical and is entirely a study of technical possibilities. To put it bluntly, it is to cheat the compiler and also lose the elegant strong type feature of C ++.
The following code is useful for real gold and silver. The C ++ class code is called in objective-C code:
1. Create a C ++ class
2. Write an adaptor's objective-C Class
3. Call the adaptor class in other objective-C logic.
Or code :)
//
// Cplusplusclass. h
// Mixcompiletest
//
// Created by biosli on 11-4-30.
// Copyright 2011 _ mycompanyname _. All rights reserved.
//
# Ifndef _ cplusplus_class_h __
# Define _ Cplusplus_class_h __
ClassCplusplusclass {
Public:
Cplusplusclass ();
Virtual ~Cplusplusclass ();
VoidFunc ();
VoidSetint (IntI ){
M_ I=I;
}
Private:
IntM_ I;
};
# Endif
//
//Cplusplusclass. Mm
//Mixcompiletest
//
//Created by biosli on 11-4-30.
//Copyright 2011 _ mycompanyname _. All rights reserved.
//
# Include<Stdio. h>
# Include"Cplusplusclass. h"
Cplusplusclass: cplusplusclass (): m_ I (0)
{
Printf ("Cplusplusclass: cplusplusclass () \ n");
Func ();
}
Cplusplusclass ::~Cplusplusclass ()
{
Printf ("Cplusplusclass ::~ Cplusplusclass () \ n");
}
VoidCplusplusclass: func (){
Printf ("Cplusplusclass func print: % d \ n", M_ I );
}
//
//Objectivecadaptor. h
//Mixcompiletest
//
//Created by biosli on 11-4-30.
//Copyright 2011 _ mycompanyname _. All rights reserved.
//
# Import<Foundation/Foundation. h>
ClassCplusplusclass;//Be careful with this Statement. Do not write it as @ class. I found this error only after half a night. Haha, smile.
@ Interface objectivecadaptor: nsobject {
@ Private
Cplusplusclass*Testobj;
}
-(Void) Objectivefunc;
@ End
//
//Objectivecadaptor. m
//Mixcompiletest
//
//Created by biosli on 11-4-30.
//Copyright 2011 _ mycompanyname _. All rights reserved.
//
# Import"Objectivecadaptor. h"
# Include"Cplusplusclass. h"
@ Implementation objectivecadaptor
-(ID) Init {
If(Self=[Super init]) {
Testobj= NewCplusplusclass ();
}
ReturnSelf;
}
-(Void) Dealloc {
If(Testobj! =Null ){
Delete testobj;
Testobj=NULL;
}
[Super dealloc];
}
-(Void) Objectivefunc
{
Testobj->Setint (5);
Testobj->Func ();
@ End
//Call example:
-(Void) Callobjectivecadaptormethod
{
Objectivecadaptor*Testobjectivecobj=[[Objectivecadaptor alloc] init];
[Testobjectivecobj objectivefunc];
[Testobjectivecobj release];
}
The above example demonstrates how to create a C ++ class, create an adaptor class of objective-C, and the entire process of the last call.
Remember a principle when writing mixed compilation code. Objective-C ++ contains objective-C.And all the keywords and symbols of C ++, so when writing cross-compilation code, do not mix keywords and symbols that are similar to the original.
References:
Specific Code restrictions: refer to the key points of objective-C and C ++ hybrid editing
Mixed compilation has many advantages. This article describes the advantages of strategies for using C ++ in objective-C Projects.