Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union of function pointers

Tags:

c++

c++11

I have a class that has different member function signatures. Based upon some requirement (to optimize execution time specifically) I need to call one the above methods at a specific time. I have plan to create a structure of below type:

#include <iostream>

class A
{
   public:
      void Show() {std::cout << "Called 0" << std::flush << std::endl;}
      int Show1() {std::cout << "Called 1" << std::flush << std::endl;}
      double Show2(char z) {std::cout << "Called 2" << std::flush << std::endl;}
      float Show3(int op, float x) {std::cout << "Called 3" << std::flush << std::endl;}
};

struct details
{
   int type ; /* methods to be called resp : 0 =Show,1=Show1,2=Show2,3=Show3*/
   union 
   {
      void (A::*fn)();
      int (A::*fn1)();
      double (A::*fn2)(char z);
      float (A::*fn3)(int op, float x);
   }fnptr;
};

int main()
{
   struct details d1 [4] ;
   d1[0].type = 0;
   d1[0].fnptr.fn = &A::Show;
   A a1;
   (a1.*(d1[0].fnptr.fn))();
   d1[0].type = 1;
   d1[0].fnptr.fn1 = &A::Show1;
   (a1.*(d1[0].fnptr.fn1))();
   d1[0].type = 1;
   d1[0].fnptr.fn2 = &A::Show2;
   (a1.*(d1[0].fnptr.fn2))('a');
   d1[0].type = 4;
   d1[0].fnptr.fn3 = &A::Show3;
   (a1.*(d1[0].fnptr.fn3))(2,3.14);
}


MINGW64 /c/work
$ c++ try.cpp -std=c++11

MINGW64 /c/work
$ ./a.exe
Called 0
Called 1
Called 2
Called 3

However I am not able to initialize the array :

struct details d1 [4] = {{0, &A::Show}, {1, &A::Show1}, {2, &A::Show2}, {3, &A::Show3}};

It states compilation error

int main()
{
struct details d1 [4] = {{0, &A::Show}, {1, &A::Show1}, {2, &A::Show2}, {3, &A::Show3}};
}

try.cpp: In function 'int main()':
try.cpp:26:87: error: cannot convert 'int (A::*)()' to 'void (A::*)()' in initialization
 struct details d1 [4] = {{0, &A::Show}, {1, &A::Show1}, {2, &A::Show2}, {3, &A::Show3}};
                                                                                       ^
try.cpp:26:87: error: cannot convert 'double (A::*)(char)' to 'void (A::*)()' in initialization
try.cpp:26:87: error: cannot convert 'float (A::*)(int, float)' to 'void (A::*)()' in initialization

How can I initialize the structure properly?

like image 858
Programmer Avatar asked Jul 26 '26 01:07

Programmer


1 Answers

However I am not able to initialize the array :

struct details d1 [4] = {{0, &A::Show}, {1, &A::Show1}, {2, &A::Show2}, {3, &A::Show3}};

When a union is initialized using that syntax, the value must correspond to its first member. In your case, the first member is fn, whose type is void (A::*)().

That line is equivalent to:

 struct details d1 [4] = {{0}, {1}, {2}, {3}};
 d1[0].fn = &A::Show;
 d1[1].fn = &A::Show1;
 d1[2].fn = &A::Show2;
 d1[3].fn = &A::Show3;

That explains the compiler error.

Documentation from the standard:

When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer-clause for the first non-static data member of the union. [ Example:

union u { int a; const char* b; };
u a = { 1 };
u b = a;
u c = 1;                        // error
u d = { 0, "asdf" };            // error
u e = { "asdf" };               // error

end example ]

like image 169
R Sahu Avatar answered Jul 27 '26 15:07

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!