0

I'm struggling to find the right answer on below question on the internet. I'm not a native C++ programmer and have more knowledge of OOP programming in PHP, Pascal, and JavaScript, but i can manage.

I want to create a class hierarchy to handle some tasks like displaying content on a LCD screen.

It looks like the following classes:

class base {
  public:
    base() { };
    virtual void display() { // Need to be called first from all child objects };
    virtual bool keyPress(int state) { //Need to be called first from all child objects };
};

class child : public base {
  public:
    child():base() {};
    virtual void display() { 
       >> call base->display() 
       // do some stuff
    };
    virtual bool keyPress(int state) { 
       >> call base->keyPress(state) 
       // check some stuff
    };
};

Most program language that i know of has some 'parent::' solution to call the inherited virtual method but i cant find anything comparable for C++.

an option that i going to use for now is:

class base {
  protected:
    virtual void _display() =0;
    virtual bool _keyPress(int state) =0;
  public:
    base() { };
    void display() { 
        // do basic stuff
        _display();
    };
    bool keyPress(int state) { 
      if (!_keyPress(state)) {
        // do basic stuff.
   };
};

class child : public base {
  protected:
    virtual void _display() { 
       // do some stuff
    };
    virtual bool _keyPress(int state) { 
       // check some stuff
    };
  public:
    child():base() {};
};

I do not like this method but it will work.

Niels
  • 28
  • 1
  • 7

1 Answers1

2

The right syntax is base::display():

class base {
  public:
    base() { };
    virtual void display() { /* Need to be called first from all child objects*/ };
    virtual bool keyPress(int state) { /*Need to be called first from all child objects*/ return 42; };
};

class child : public base {
  public:
    child():base() {};
    virtual void display() { 
       base::display();
       // do some stuff
    };
    virtual bool keyPress(int state) { 
       return base::keyPress(state);
       // check some stuff
    };
};

However, if it is the same in all child classes you better let base call its methods like you do it in your second code. It is not clear why you "do not like this method". It works, does what you want, and avoids lots of duplicate code and decreases chances for mistakes in the derived classes. Just note that the virtual methods need not be protected, because the derived classes are not supposed to call them directly, you can make them private: https://godbolt.org/z/1qjooKq85 (perhaps that is what you didn't like?).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • About your question there are two reasons for me: 1) It is a habit that i learned from my pascal/delphi developing days and i use it now also in other language, this way i have more control in the child class of i want to call the parent method or not. 2) In some of the child classes i do not need the base functionality to execute. My second solution prevent that. – Niels Jan 31 '22 at 12:20
  • Perfect it works, the strange thing is that i tried it before and my ESP32 got a panic attack. going to test it again and see of i can figure out what did go wrong. – Niels Jan 31 '22 at 12:46