473,480 Members | 2268 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calling C++ class methods from C

1 New Member
If you want to call class methods from C++ from within C, use a class wrapper.
One advantage of this method is that the C++ class remains unmodified, and can
even exist in a library.

First, let's define the C++ class, "Circle". For simplicity, we'll do everything
in a .h file, but it works just as well for classes declared in a .h and defined
in a .cpp file.
Expand|Select|Wrap|Line Numbers
  1. // Circle.h - a C++ class
  2.  
  3. #ifndef CIRCLE_H
  4. #define CIRCLE_H
  5.  
  6. class Circle {
  7.     public:
  8.         Circle(float radius):_radius(radius) {}
  9.         float getArea() { return 3.14159 * _radius * _radius; }
  10.     private:
  11.         float _radius;
  12. };
  13.  
  14. #endif
Now let's declare a C++ wrapper for this class, which declares extern C methods,
which can be used from within C. This code must compile in both C++ and C files.
Use void * to point to class instances. Note the use of #ifdef __cplusplus.
Expand|Select|Wrap|Line Numbers
  1. /* Circle_C.h - must compile in both C and C++ */
  2.  
  3. #ifndef Circle_C_H
  4. #define Circle_C_H
  5.  
  6.     #ifdef __cplusplus
  7.     extern "C" {
  8.     #endif
  9.  
  10.     extern void *Circle_C_new(float radius);
  11.     extern void  Circle_C_delete(void *circle);
  12.     extern float Circle_C_getArea(void *circle);
  13.  
  14.     #ifdef __cplusplus
  15.     }
  16.     #endif
  17.  
  18. #endif
Now define the extern functions. These will only be compiled as C++, so classes
can be referenced.
Expand|Select|Wrap|Line Numbers
  1. // Circle_C.cpp - extern C function definitions
  2.  
  3. #include "Circle_C.h"
  4. #include "Circle.h"
  5.  
  6. extern void *Circle_C_new(float radius) {
  7.     return new Circle(radius);
  8. }
  9.  
  10. extern void Circle_C_delete(void *circle) {
  11.     Circle *c = (Circle *)circle;
  12.     delete c;
  13. }
  14.  
  15. extern float Circle_C_getArea(void *circle) {
  16.     Circle *c = (Circle *)circle;
  17.     return c->getArea();
  18. }
We can now use these extern C functions to access the C++ classes from C. Here
is a C main function example:
Expand|Select|Wrap|Line Numbers
  1.  /* mixed.c - a C file that accesses C++ methods */
  2.  
  3. #include <stdio.h>
  4. #include "Circle_C.h"
  5.  
  6. void main() {
  7.     float radius = 1.5;
  8.  
  9.     // Get a pointer to a Circle object
  10.     void *circle = Circle_C_new(radius);
  11.  
  12.     // Pass the Circle object to the wrapper methods
  13.     float area = Circle_C_getArea(circle);
  14.  
  15.     printf ("Circle of radius %f has area %f\n", radius, area);
  16.  
  17.     // Free the Circle object memory
  18.     Circle_C_delete(circle);
  19. }
Here is a sample make file which creates a "mixed" executable.
Expand|Select|Wrap|Line Numbers
  1. # Makefile - creates the executable "mixed".  Must link the stdc++ library
  2.  
  3. mixed: main.o Circle.o
  4.     gcc -lstdc++ -o mixed main.o Circle_C.o
  5.  
  6. # Compile main as C
  7. main.o: main.c
  8.     gcc -c main.c -o main.o
  9.  
  10. # Compile Circle_C as C++
  11. Circle_C.o: Circle_C.cpp
  12.     g++ -c Circle_C.cpp -o Circle_C.o
Submitted by Jim Rogers
Nov 7 '11 #1
2 14166
prabu jayaraman
1 New Member
Thanks a lot. It really helped for my work . YOU ROCKS....
Jun 25 '15 #2
VFSAS
1 New Member
GREAT! It really helped;
Jun 13 '18 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

37
17028
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the...
12
1909
by: Thomas Heller | last post by:
I once knew how to do it, but I cannot find or remember it anymore: How can I attach *class* methods to a type in C code, when I have a function PyObject *func(PyObject *type, PyObject *arg);...
1
2340
by: Josh Close | last post by:
Is there a way to call a class method without an instance of that class? class myClass: def printSomething(message): print message myClass.print() That's basically what I want to do. Is...
3
1530
by: Danny Shevitz | last post by:
Howdy, I am trying to call class methods that have been created via a "type" function. I have enclosed a simplified example that shows what I am trying to do. In particular I am calling from the...
2
2074
by: Craig Ringer | last post by:
Hi folks I've been doing some looking around, but have been unable to find out how to implement class methods on Python objects written in C. "Why are you using C?" you ask? Yeah, so do I....
18
6928
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
3
3914
by: acg | last post by:
If you have a class with a public method, and another class which will want to call this method, is there a way to determine the type of the calling class within the method being called? For I...
10
1618
by: David Hirschfield | last post by:
Here's a strange concept that I don't really know how to implement, but I suspect can be implemented via descriptors or metaclasses somehow: I want a class that, when instantiated, only defines...
5
6840
by: Dylan Moreland | last post by:
I'm trying to implement a bunch of class methods in an ORM object in order to provide functionality similar to Rails' ActiveRecord. This means that if I have an SQL table mapped to the class...
2
4034
by: ambujnema | last post by:
When I am calling C# dll into Vb6.0 through Server.CreateObject (“ClassLibrary1.Class1”). I stuck at one place; I have to pass some parameter in that class, Class contain two methods, in which...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4495
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.