C++ class pointers to another class

Hello, i am developing a TTY terminal parser library for arduino *1, so i got the parser for the escaped commands(tty console codes), and the related hardware specific display operations.

The idea is to create an easy swappable hardware-related display class/library.

The way i am now trying is to create empty pointers to display operations, and execute them only if previously assigned.


else if (caracter == SGR)//Set SELECT_GRAPHIC_RENDITION '0' = RESET. '10' = DEFAULT_FONT. '7' = INVERSE.
{
	if(this->sGR != nullptr)this->sGR(argc,argv);
}

And here is the code:

SH1106_DisplayOperations.CUP(int argc,int *argv[]) <-- the code is here

But i am not nailing the declaration or the assign, or i need some typedef... IDK, the compiler and linker are getting really mad, the usual error goes like: Cannot convert 'void (MyClass::)()' to 'void ()()

I shall confess i am not a senior c++ dev :(, so i praise c++ seniors 4 an answer.

How shall be done?
If it's too hard?
Is there a better way?

*1 console_codes(4) - Linux manual page

Why would you use pointers to functions if you use a class based approach?

In OOP you would probably define a base class and define some methods as virtual and the different hardware would use different subclasses

See how Stream inherits from Print and HardwareSerial from Stream

1 Like

Pointers method is useful for partial implementations, so, only developed and assigned functions are called.

'Cause there are MANY display operations, and only a subset will be developed in any case, pointers provide an automatic way to discriminate via nullPtr.

Also, the terminalParser class can be developed independently mantaining full compatibility providing support for future implemented code for escape codes, so can be published only parsing the most common subset.

Thanks.

You better declare a common functions (API) base class and use multiple inheritance to derive classes with specific displays. Like the Stream class provides several methods that can work only in derived classes for Serial, Wire etc.

The base class by itself can do almost nothing, but as soon as you instantiate a derived class for your current display, based also upon that display library, your code can propagate all commands to that library.

You would do that in C.

A base class would be used in OOP. Pointers to non static member functions are messy.

Note that the optimizer will likely remove calls to functions doing nothing, so your performance will possibly be better than checking for a null pointer if that can be done at compile time

1 Like

Already tried declaring the methods pointing to an external class, but did not succeed.
Maybe wrong declaration, don't remember exactly, been 100+ days trying, tried many stuff.

static void (SH1106_DisplayOperations::*moveCursorX)(unsigned int,int[]); // function pointer declaration

How this shall be done?

Nice idea, but d'oesn't seem applicable in this case 'cause it is the base class which calls the derived.

myHardwareDisplay : terminalParser

How terminalParser would call a myHardwareDisplay method ?

Thanks.

Not sure what you mean.

Abstract / pure virtual ?

https://en.cppreference.com/w/cpp/language/abstract_class

1 Like

here is an example with a virtual member function

class TerminalParser {
  public:
    virtual void whoAmI() {
      Serial.println(F("not implemented"));
    }
};

class HardwareDisplay : public TerminalParser {
    void whoAmI() {
      Serial.println(F("HardwareDisplay"));
    }
};

class SoftwareDisplay : public TerminalParser {
    void whoAmI() {
      Serial.println(F("SoftwareDisplay"));
    }
};

class PartialDisplay : public TerminalParser {
  // whoAmI is not implemented, the default function will be called in the parent class
};

HardwareDisplay h;
SoftwareDisplay s;
PartialDisplay p;

TerminalParser* myDisplays[] = {&h, &s, &p};

void setup() {
  Serial.begin(115200); Serial.println();
  for (auto & aDisplay : myDisplays) aDisplay->whoAmI();
}

void loop() {}

you can run it here in the simulator

the serial monitor will print

HardwareDisplay
SoftwareDisplay
not implemented

showing that child member functions were call despite the fact that the array was holding TerminalParser instances pointers ➜ the method that was called was actually based on the real instance type.

1 Like

If you are pointing to class member function you have to pass instance, otherwise declare function static or point to global function or pass lambda. As you have shown no specifics, a non specific answer you shall get

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.