<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://test-devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/f/nordic-q-a/88011/issue-with-calling-c-static-class-member-function</link><description>Hi, 
 
 I have a C++ class with code as in below: 
 
 
 This I need to call in a .c file, which I did with extern &amp;quot;C&amp;quot; definitions with a C wrapper as in below: 
 I am getting errors if I compile this, same above static class member function I am able</description><dc:language>en-US</dc:language><generator>Telligent Community 13 Non-Production</generator><lastBuildDate>Fri, 20 May 2022 14:51:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://test-devzone.nordicsemi.com/f/nordic-q-a/88011/issue-with-calling-c-static-class-member-function" /><item><title>RE: Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/thread/368858?ContentTypeID=1</link><pubDate>Fri, 20 May 2022 14:51:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4af48c9d-e75a-4318-90bc-bf1f71fcf622</guid><dc:creator>user111786</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;I think you would need some wrappers in your code. You could try something like this&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//class_header.h file
class Model_Server
{
    public:
           static void Command_Handler( );
};


//wrapper.h file
#ifdef __cplusplus
extern &amp;quot;C&amp;quot; {
#endif 
void wrapper();
#ifdef __cplusplus
}
#endif


//.cpp file
#include &amp;quot;class_header.h&amp;quot;
#include &amp;quot;wrapper.h&amp;quot;

void Model_Server::Command_Handler( );
{
    // some print for testing goes here
}

extern &amp;quot;C&amp;quot; void wrapper()
{
    Model_Server::Command_Handler();
}


//main.c
#include &amp;quot;wrapper.h&amp;quot;

void main(void)
{
	wrapper();
}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/thread/368716?ContentTypeID=1</link><pubDate>Fri, 20 May 2022 06:58:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f5010e2-0fc3-47a3-be34-8f71bfce11fd</guid><dc:creator>user108934</dc:creator><description>&lt;p&gt;Hello &lt;a class="internal-link view-user-profile" href="https://test-devzone.nordicsemi.com/members/dejans"&gt;dejans&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="111786" url="~/f/nordic-q-a/88011/issue-with-calling-c-static-class-member-function/368670#368670"]it is possible to use static function which forwards to the class method by using static class pointer. However, this would require assigning the pointer so that it is known which class instance should be used when static pointer is called[/quote]
&lt;p&gt;Thanks, I tried this out, but I am unable to call static function inside the model_handler.c file.&lt;br /&gt;To call a static function, I need to write a wrapper C++ file &amp;amp; Wrapper API, this wrapper API i can call in&amp;nbsp;&lt;span&gt;model_handler.c.&lt;br /&gt;&lt;br /&gt;For example:&lt;br /&gt;1. If I have C++ static API as in below in a file model_server.cpp:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;#ifdef __cplusplus
extern &amp;quot;C&amp;quot; {
#endif


static void Model_Server::Command_Handler()
{
	int a;
}

#ifdef __cplusplus
}
#endif
&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;2. I need to write a wrapper.cpp for a wrapper API as in below, by marking C++ includes with extern &amp;quot;C&amp;quot;:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;extern &amp;quot;C&amp;quot;{
#include &amp;quot;model_handler.h&amp;quot;
}

void wrapper_file_API()
{
    Model_Server::Command_Handler();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;3. Then finally I can call the first API into .c file as in below with wrapper:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void wrap()
{
	wrapper_file_API();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;As you can see my code increases if I add wrappers like this,&lt;br /&gt;&lt;br /&gt;If I call the C++ class member static function straightaway in .c file as in below:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;void Wrap()
 {
  Model_Server::Command_Handler();
 }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;GCC doesn&amp;#39;t recognize the static member function syntax &amp;quot;Model_Server::Command_Handler();&amp;quot; and gives me build errors.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="text-decoration:underline;"&gt;&lt;strong&gt;So, can you please suggest how I can get to call&amp;nbsp;the static member function and assign the handler pointers without writing wrappers...?&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I am struggling with the syntax&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Thanks,&lt;/strong&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/thread/368670?ContentTypeID=1</link><pubDate>Thu, 19 May 2022 14:54:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:425d0e1f-dd77-4659-9e95-1b78402bfe14</guid><dc:creator>user111786</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;C++ methods cannot be used directly as callbacks because of the need for having a class instance. However, static functions should be able to run even if they are defined in C++ file.&lt;br /&gt;When mixing C and C++, it is possible to use static function which forwards to the class method by using static class pointer. However, this would require assigning the pointer so that it is known which class instance should be used when static pointer is called. You could take a look at this&amp;nbsp;&lt;a href="https://social.msdn.microsoft.com/Forums/en-US/c7e64313-befd-4c03-8164-f22f0e400d1a/problem-with-class-function-pointer-staticnonstatic?forum=vclanguage"&gt;discussion&lt;/a&gt;.&amp;nbsp;You can also read more&amp;nbsp;&lt;a href="https://stackoverflow.com/questions/36969471/c-how-to-make-function-pointer-to-class-method"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/thread/368317?ContentTypeID=1</link><pubDate>Wed, 18 May 2022 11:42:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4647704d-d5c9-4727-be0c-50ba45ac6046</guid><dc:creator>user108934</dc:creator><description>&lt;p&gt;Hello&amp;nbsp;&lt;a class="internal-link view-user-profile" href="https://test-devzone.nordicsemi.com/members/dejans"&gt;dejans&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks I was able to work it out.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://test-devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/pastedimage1652791032028v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Was because I had to mark C++ include files with extern &amp;quot;C&amp;quot;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://test-devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1652793480700v2.png" /&gt;&lt;/p&gt;
&lt;p&gt;This error was because I shouldn&amp;#39;t call C++ symbols like class names directly in C file, had to write a C++ wrapper file then call wrapper APIs into C file.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But you can help me with one thing,&lt;br /&gt;That is, I want to write my model handlers in C++ and the assign those C++ model handlers to model handler structure. like below:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;static const struct bt_mesh_health_srv_cb health_srv_cb = {
	.attn_on = attention_on, //C++ function
	.attn_off = attention_off,//C++ function
};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Can you suggest how to do this..?&lt;br /&gt;&lt;br /&gt;If you have a reference code, then please point me.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Thanks,&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/thread/368299?ContentTypeID=1</link><pubDate>Wed, 18 May 2022 11:14:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5576f2a7-f440-4d8c-8876-2116aa3940bd</guid><dc:creator>user111786</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Could you please show how your code looks like now that everything is in the same file?&lt;br /&gt;&lt;br /&gt;Could you also show how you call static class member function from C file?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/thread/368154?ContentTypeID=1</link><pubDate>Tue, 17 May 2022 13:18:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c9166d54-52c8-463b-b194-5621d3fcabf2</guid><dc:creator>user108934</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;I was able to mitigate the cpp file error by having the Class&amp;nbsp; declaration &amp;amp;&amp;nbsp;&lt;span&gt;static class member function in the same file,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;But when I call the&amp;nbsp;static class member function from C file, I am getting the error:&lt;br /&gt;&lt;img src="https://test-devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1652793480700v2.png" alt=" " /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issue with calling C++ static class member function</title><link>https://test-devzone.nordicsemi.com/thread/368149?ContentTypeID=1</link><pubDate>Tue, 17 May 2022 12:37:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45553498-8922-45f6-a615-00eed68f1c76</guid><dc:creator>user108934</dc:creator><description>&lt;p&gt;I read online that at cpp file, i need to declare extern &amp;quot;C&amp;quot;, which I did, but I am getting error in Class declaration now:&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;//C++ .h file
#ifdef __cplusplus
extern &amp;quot;C&amp;quot; {
#endif

class Model_Server{
    public:
           static void Command_Handler( );
};

#ifdef __cplusplus
}
#endif

#endif&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;error:&lt;br /&gt;&lt;img src="https://test-devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1652791032028v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>