0

hi, in the code below :

#include <iostream>
using namespace std;

struct deneme
{

  int a;
  void (* myFunction)(int a);
  
};

void function1(int a)
{
  cout << a << endl;
}

void function2(int a)
{
  cout << a + a << endl;
}
int main()
{
 
  deneme myDeneme[] = {{1,function1},{2,function2}};
  
  myDeneme[0].myFunction(myDeneme[0].a);
  myDeneme[1].myFunction(myDeneme[1].a);
  
  return 0;
}

i get the output:

1
4
which is correct.
My question is : the instance of my struct already includes the integer member "a", instead of passing it to the function as a parameter, is there any way of grabing that information from the struct it self?

Thanks

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by serkan sendur
1
#include <iostream>
using namespace std;

struct deneme
{
  int a;
  void (* myFunction)(int a);
  void look4adventures() { myFunction(a); }
};

void function1(int a)
{
  cout << a << endl;
}

void function2(int a)
{
  cout << a + a << endl;
}
int main()
{
   deneme myDeneme[] = {{1,function1},{2,function2}};
  
  myDeneme[0].look4adventures();
  myDeneme[1].look4adventures();
  
  return 0;
}
Votes + Comments
so smart
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.