help needed to read some C++ code

402DF855

Joined Feb 9, 2013
224
C:
template <typename R, typename... ArgTs>
class Callback<R(ArgTs...)> {
public:
  /** Create a Callback with a static function
  *  @param func  Static function to attach
  */
  Callback(R(*func)(ArgTs...) = 0)
  {
      if (!func) {
          memset(this, 0, sizeof(Callback));
      } else {
          generate(func);
      }
  }
...
I've not much experience with this type of template usage, but R is a type which "func" returns, and A0.... are parameters of the call to "func". Note that R is defined in the template declaration. The "=0" appears to specify that func==NULL if the callback is declared with no parameters, i.e. 0 is the default value for the parameter to the callback constructor.
 

Thread Starter

bug13

Joined Feb 13, 2012
1,889
C:
template <typename R, typename... ArgTs>
class Callback<R(ArgTs...)> {
public:
  /** Create a Callback with a static function
  *  @param func  Static function to attach
  */
  Callback(R(*func)(ArgTs...) = 0)
  {
      if (!func) {
          memset(this, 0, sizeof(Callback));
      } else {
          generate(func);
      }
  }
...
I've not much experience with this type of template usage, but R is a type which "func" returns, and A0.... are parameters of the call to "func". Note that R is defined in the template declaration. The "=0" appears to specify that func==NULL if the callback is declared with no parameters, i.e. 0 is the default value for the parameter to the callback constructor.
Thanks for the extra explanation, it really helps!!
 

BobaMosfet

Joined Jul 1, 2009
1,229
Function pointers in C are a lot more straightforward, than what you're looking at for C++. I mean seriously. A0-A4 are arguments, but they are _labeled_ to match assembly language registers (that's what they are likely referring to).

I strongly recommend you wrap your mind around these concepts in C before you jump to C++. It's easier. C++ is made for modeling (things like knee joints), it's not good for learning the things you're trying to learn because it expects you already know them.
 
Last edited:

Thread Starter

bug13

Joined Feb 13, 2012
1,889
I strongly recommend you wrap your mind around these concepts in C before you jump to C++. It's easier. C++ is made for modeling (things like knee joints), it's not good for learning the things you're trying to learn because it expects you already know them.
Again, thanks for your helpful advice. I do think I understand things like function pointer, pointer, address, stack and heap in C in a (bare metal and RTOS) embedded system, but most likely not as good as you.

For example, with the help of function pointer, I wrote this simple co-operative task scheduler that I use in most of my main powered application.
https://github.com/jmswu/schedular

However, for whatever reason, things are not always obvious for me when I first look at it. Often, a little pointer from someone like you and others with more experience can be a great help to me.
 

MrSoftware

Joined Oct 29, 2013
1,936
In a C function call, the stack is used to pass parameters. There's lots of good reading on it. If you're using a good IDE such as Microsoft Visual Studio, you can write a simple function call then turn on the disassembly window and step through the assembly as the function call occurs to see what's happening. Here's a google search that will lead to some good reading:

https://www.google.com/search?safe=...hUKEwjQ3sGb-rTkAhUGi6wKHeuWCOgQ4dUDCAs&uact=5
 
Top