Use function or function pointer when dependency is inverse? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm developing a MCU (MicroController Unit) development system (which means limited space and low speed, and no operating system in my project). It is composed of 2 kinds of MCUs which play different roles in the system, and their programs are different.
They use the same underlying (static) library written in C. The library needs a callback function when it receives data from bus. The callback functions should be in the clients (C for one the C++ for another), and are also different for two programs.
What should the structure be like? Here are two solutions.
Declare a function in the underlying library and define it in the client. The library just calls this function.
Define a function which take an function pointer as a parameter and then assign to a static variable. The library calls the function by the function pointer.
Currently I'm using the second solution just because it seems to be more elegant. However, the first has higher performance, and is safer (linker will give an error if the definition doesn't exist), while the first involves an indirection and is not that safe (if I forget to register the function).
Which one should I choose? Or are there any better solutions?

Related

Is it okay to shorthand a function call using a macro? C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am slowly writing an emulator for a gameboy using C++, I am currently working on the CPU side of things. I have written a generic function that takes any two registers of the CPU and returns as a Word data type. As it is necessary to access individual registers and also a combination of registers.
const Word get_word(Byte *registerOne, Byte *registerTwo)
{
return ((*registerOne << 8) | *registerTwo);
};
calling this function gets tedious as you have to specify each register
get_word(&this->registers.h, &this->registers.l)
My question is if it okay to define a macro like so
#define get_HL() get_word(&this->registers.h, &this->registers.l)
since now I can call it using
get_HL()
The reason why I want to do it like this since I don't want to create more private/public functions that just perform function calls.
I have tried compiling and it seems to work as it should since its just a pre-processor macro but I am not sure of the design implication
EDIT:
Okay I mean there are glaring flaws with this and you should just make a function, just as much work to make a function or write a macro.
const Word get_HL() { return this->get_word(&this->h, &this->l); };
Let this be a post for people who had the same idea and hopefully stop making the same mistake
No, this isn't OK in my opinion. It hides what arguments you're passing into the function, and macros don't respect scopes and as such are highly susceptible to name conflicts. This seems like an ideal use case for a non-static member function.

lambda functions vs functors [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
when I am going through the lambda function, I have seen people comparing lambda with functors & I came across a statement
users don't have to clutter their code with small functors in some accessible scope.
My doubt is
what is the problem in having small functors in some accessible scope
isn't it good idea to have a single function (functor actually) & reuse it across multiple files in our project.
Thanks.
it is unnecessary if you have to use each only once. Lambdas usually makes the code more readable, the function is defined exactly at the place it is needed.
this is not always the case, a function may be called at only one place. Of course if you needed it at different places a functor may be more appropriate.

Can we say that a standalone function provides Abstraction? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am learning about Abstraction, and as I have understood so far, Abstraction is basically providing an interface of how to use an object while hiding the implementation details. But does the concept of Abstraction only applies to OOP, I mean if we think of a standalone function (without being a part of a class), using a function is indeed only using its interface without actually caring of how the function is implemented.
Of course a function provides abstraction.
Why would a bunch of functions and a this pointer provide abstractions while a single function without a this pointer would not?
If, for example you have a function sort() which sorts some data, it abstracts from the concrete sorting algorithm. If you have a function which is the entry point to a huge piece of code consisting of thousands of sub-functions called in the context of that function, it can even be very abstract. Example: GetRouteFromCurrentLocationTo(...). A router, a position sensor, some geographical database... all that abstracted to a single function name.
Why would it be more of an abstraction if you wrote instead: NavigationSystem navSys; navSys.GetRouteFromCurrentLocationTo(...); ?

Is it possible to send a pointer to function via socket? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two processes of the same program, possibly running on two different machines.
I'd like the process P2 to receive a function pointer from process P1 via socket.
Is is possible? Is it a good practice?
EDIT: more interesting would be to send the code of the function too, but I'm skeptic about this. Would it be possible?
You can send a function pointer from one process to another, the same way you can send a pointer to some other object.
The problem is that the pointer may not actually point to the function as it exists in the target process. Especially if the OS is protecting itself with things like ASLR.
You could also send the code across, provided you had some way of figuring out where it ended, and that it was position independent code, and that your environment allowed you to write arbitrary data to memory and then call it.
But, to be honest, there are better ways to achieve what you seem to want, such as the use of RPC (remote procedure calls), in a more portable manner.

standards of using and naming of pointer to function as delegates in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
There are certain questions in my mind:
What is the standard for naming pointer to functions as delegates?
What is the best way to define signature of functions in this matter?
What are the techniques of maintaining safety and stability of code
when using this pointers?
There is no standard for such naming, only conventions that vary from project to project (or company to company). Common rules include avoiding leading underscores in such names (reserved for the standard library)
There is no best way to "define a signature", it's the same as for any other functions: choose explicit and clear names for both the method name and its arguments.
The best advise is to avoid explicit pointers to functions, and prefer using std::function , a powerful polymorphic function wrapper.