473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Array of Functions pointing to in Class functions with arduino

2
Hello all,

At the moment im trying out with pointing to an array of functions. I got this working as following:
Expand|Select|Wrap|Line Numbers
  1. typedef void (* functionPtr) ();
  2.  
  3. functionPtr functions[2][2]={{do11,do12}, {do21,do22}};
  4.  
  5.  
  6. void do11(){DEBUG_PRINTLN("11");}
  7. void do12(){DEBUG_PRINTLN("12");}
  8. void do21(){DEBUG_PRINTLN("21");}
  9. void do22(){DEBUG_PRINTLN("22");}
  10.  
  11.  
  12. void loop(){
  13.          A=0;
  14.          B=1;
  15.          functions[A][b]();
  16. }
  17.  
But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:

error: argument of type 'void (Basic::)()' does not match 'void (*)()'

Any ideas?
May 3 '13 #1

✓ answered by weaknessforcats

You are stuck here.

The class member functions must have all arguments the function needs at compile time. One argument that's needed is the address of the object to be used in the function call. Since this is not known at compile time, the compiler secretly adds the this pointer as the first argument. Therefore, each member function has an invisible class* as the first argument.

But if you get sneaky and add this yourself, your member function will have two class* arguments. So things still won't work for you.

You can add a class static function but now you don't have access to the class member data because the static function is not a member function.

This all boils down to advising you that if you need to choose your function by address you are performing polymorphism and for that you need a class hierarchy and virtual functions.

C++ is all about data protection.

Otherwise, you will need to switch to C which has no real data protection and will not prevent you from doing anything you want.

2 2761
weaknessforcats
9,208 Expert Mod 8TB
You are stuck here.

The class member functions must have all arguments the function needs at compile time. One argument that's needed is the address of the object to be used in the function call. Since this is not known at compile time, the compiler secretly adds the this pointer as the first argument. Therefore, each member function has an invisible class* as the first argument.

But if you get sneaky and add this yourself, your member function will have two class* arguments. So things still won't work for you.

You can add a class static function but now you don't have access to the class member data because the static function is not a member function.

This all boils down to advising you that if you need to choose your function by address you are performing polymorphism and for that you need a class hierarchy and virtual functions.

C++ is all about data protection.

Otherwise, you will need to switch to C which has no real data protection and will not prevent you from doing anything you want.
May 5 '13 #2
Proos
2
Thanks for that information then I quess I just need to think of a different way to do it. Thanks alot.
May 6 '13 #3

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

Similar topics

10
by: Evangelista Sami | last post by:
hello i haven't touch any C code for long time and i dont remember how to declare an array of pointers on functions. i have tried this : ----------------------- 35 void firing_1 36 (firing f,...
3
by: Scott McNair | last post by:
Hi, I'm trying to populate a 2-dimensional array by assigning each item its own instance of a class, for example: Dim SubSector(8,10) As New SystemClass ....where SystemClass is a class...
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
3
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? ...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
4
by: Miro | last post by:
Vb2003, im still learning vb.net but I do not understand my output from this logic. If someone can help me out here. Cor Ligthert, you I believe were on the right track of what Im trying to...
7
by: recover | last post by:
class Obj { public: Obj(int a){} } class MyContainer { private: Obj m_obj;
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
7
by: beginner | last post by:
Hi Everyone, I have encountered a small problems. How to call module functions inside class instance functions? For example, calling func1 in func2 resulted in a compiling error. "my module...
23
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.