Is there a way to alias functions in a library?

I have this library (shortened for brevity):

#ifndef BN_h
#define BN_h
#pragma once
#include "Arduino.h"

class BN {
    // bool BlockNot::resetTimer;

public:
    BN(int a = 1, int b = 2);

    bool hasPassed(int c = 0);

private:
	
	int aNumber;

};
#endif
#include <Arduino.h>
#include <BN.h>

BN::BN(int a, int b)
{
	//Various code
}

bool BN::hasPassed(int c) {
	return aNumber == c;
}

I would like to be able to call the hasPassed function using different names (for code readability). I went through different articles on function pointers but I can’t seem to get any of the examples to work inside a library.

Does anyone know how to properly create function pointers inside a library?

Mike

This is a good article on pointers to member functions: Standard C++
They are indeed different than pointers to regular functions.

inline bool BN::differentName(int c) {
	hasPassed(int c);
}

?

PS Please don't make a library with a short an ambiguous name like BN...

I would like to be able to call the hasPassed function using different names (for code readability).

The simplest way would be to put the required functions with their different names into the library and have them call the common hasPassed() function

As a matter of interest what sort of names did you have in mind for the functions ?

Use the pre-processor. Say I wanted to alias Serial.print with something:

#define DEBUG Serial.print

and then

DEBUG("some text");

becomes

Serial.print("some text");

All it's doing is a text based find and replace, so be careful how you use this stuff. But as long as you're just substituting one name for another it should work.

Delta_G:
All it’s doing is a text based find and replace, so be careful how you use this stuff. But as long as you’re just substituting one name for another it should work.

How does that translate to #define(ing) a function that has arguments that need to get passed into it and then make decisions based on those arguments? Are you saying something like this should work in my libraries .cpp file?

#include <Arduino.h>
#include <BN.h>

#define isDone BN::hasPassed

BN::BN(int a, int b)
{
 //Various code
}

bool BN::hasPassed(int c) {
 aNumber = c;
}

so that once the library is instantiated, I can just use the name given in the #define statement, like this?

BN bn(10,20);

void someFunction() {
    if (bn.isDone(5)) doSomething; else doSomethingElse;
}

? ? ? ? ?

It’s a text based find and replace. Just like hitting control F and doing a find and replace. So this:

#define isDone BN::hasPassed

plus this:

if (bn.isDone(5)) doSomething; else doSomethingElse;

becomes:

if (bn.BN::hasPassed(5)) doSomething; else doSomethingElse;

So that’s definitely not what you want.

Yes, macros can take arguments. You have to be very careful with them because they’re not evaluated the same way things are in functions.

Generally it would be best to use the API you’re given and not to try to do this. What particular problem are you running into? Maybe you could create wrapper functions for everything.

I suspect that it's advisable that hasPassed actually returns a bool :wink:

sterretje:
I suspect that it's advisable that hasPassed actually returns a bool :wink:

Of course it does...

EasyGoing1:
Of course it does...

bool BN::hasPassed(int c) {
 aNumber = c;
}

Where?

Guy, why are we messing with macro's here...

What is wrong with my suggested solution in post #3 ?

@UKHeliBob, indeed! Same as I suggested in reply #2 (but with less words..)

Delta_G:

bool BN::hasPassed(int c) {

aNumber = c;
}




Where?

My bad, i fixed it.