Using C++ class function in C
Credit: https://www.testbytes.net/blog/programming-memes/

Using C++ class function in C

Hi Can you use a C++ class member function in C source file, Yes or No??

If No then go ahead and take 5-10 mins or if yes, then also check it once at-least, if you have got something different please share feedback.


One way which I am going to explain is using pointers! Our task involves interaction between two different kind of sources which can easily be handled by accessing the memory. Some core C++ application developers might not like it (?) but in Embedded domain it is useful.


I have taken Logging functionality as an example to explain it which uses UART, the class design and interface can be used with different peripherals such as SD Card, Network etc (?).


A Logger class which has a object like ‘cout’ in C++ named ‘logger’ to log the events occurring in system which uses FreeRTOS utilities for synchronization and peripheral access has been utilized.

There may be other ways but I have not considered them in this document, this one is easy!


Let’s get to the source and learn the methodology 😊

1. First, we need to declare the public member function which can be accessed without creating a class object.

static inline void write( unsigned char *p_data, unsigned short length );
No alt text provided for this image

Can remove inline if you do not want the function implementation in header file of class and define it in source file. (declared in <class_header.h>)


2. Create a private function which is used to write data in class and would be used by our static ‘write’ function as well. (Below functions are declared in <class_header.h> file)

virtual void _write( unsigned char *p_data, unsigned short length )

I have put ‘_’ in front to represent it as an internal function as in libraries.

This function will be used by two member functions:

a.      virtual Logger__& operator<<( Logger__& end(Logger__&) ) [Class public function which is used to trigger the send operation]

b.      static inline void write( unsigned char *p_data, unsigned short length ) [To be accessed using function pointer]

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Actual write to peripheral happens in this function.


3. Create a function pointer which points to the static function of class object (for this example)

void (*p_Logger__) ( unsigned char *p_data, unsigned short length ) = Logger__::write;

It is a global function pointer which is compiled within extern “C” block in C++ file <class_source.cpp>.

No alt text provided for this image

(Other method to declare and use function pointer is described in source file)

All the above operations ([1,2] & 3) were done in class header and source file (<class_header.h> and <class_source.cpp>) respectively.

Now we need to include the function pointer pointing to C++ Class member function in other C source files to use it.

extern void (*p_Logger__)( unsigned char *, unsigned short );

We can include this function pointer reference in some header and include that header in source file also.


4. Time to use the C++ class member function in C source file in which function pointer reference has been included (user can choose the way).

Create a test C source file or main file and include function pointer reference as shown in image below:

No alt text provided for this image

We are done!!

Now we can use the p_Logger__ function pointer in this file anywhere to log events or anything on UART terminal.

END


I have not checked whether it is compliant with some standard or not but Useful and Fun!

I hope you found the above data insightful and helping. You can see the source code for your reference. (Link to project -> https://github.com/embeddedcrab/STM32F429_Disco)

You can find the documented version here https://github.com/embeddedcrab/STM32F429_Disco/blob/master/Using_C%2B%2B_ClassFunction_in%20_C.pdf

Hemant Sharma

Enjoy! Happy to Share and Help 😊

To view or add a comment, sign in

More articles by Hemant Sharma

Others also viewed

Explore content categories