Skip navigation.
 
mlRe: using C++ class in the Obj-C file (FAILED...)
FROM : Brady Duga
DATE : Wed Feb 20 20:45:25 2008

On Feb 17, 2008, at 3:49 AM, Leopard x86 wrote:
>
> The include method is forbidden, for if I included the foo.h, the 
> previous files included by foo.h throwed out lots lots of compile 
> error. Very horrible...


It might make the most sense to figure out what those problems are and 
fix them. Often it is a simple matter of needing to include another 
file first.

>
>
> Then I want to use a trick to solve this: because the UNIX library 
> has been compiled into a dylib file, I can use the C++ keyword 
> "extern" to let the compiler think the class has been defined. When 
> the code compiled, the compiler will automatically find the symbol 
> defined in the dylib file and solved the linker.


Hmm... I'm not sure you understand what extern is for.
>
>
> extern "C" {
>     class foo;
> };


Basically, this code tells the compiler to handle the linkage to 
functions and variables in the brackets as though the current language 
is C instead of C++. Of course, there are no functions or variables in 
those brackets, just a forward declaration  of a class. You can drop 
the extern "C" and just forward declare class foo, but that probably 
won't help. If all you ever do in your code is pass the pointer around 
(you never call 'new foo' or access any of the classes members) then 
it might be ok. However, if you actually need to *use* an object of 
class foo, you need the full definition of the class.

Of course, this is a very non-Cocoa-ish question. Heck, I doubt it is 
even related to Objective-C[++]. You might want to take further 
questions to a more appropriate place (and no, I don't know where that 
place is - perhaps someone else can recommend a list).

--Brady

Related mailsAuthorDate
mlusing C++ class in the Obj-C file (FAILED...) Leopard x86 Feb 17, 12:49
mlRe: using C++ class in the Obj-C file (FAILED...) Brady Duga Feb 20, 20:45
mlRe: using C++ class in the Obj-C file (FAILED...) Herb Petschauer Feb 20, 21:00