Search code examples
c++linuxclassgpio

Creating a property that is the same for all objects


How to create a property that will be the same for all objects created by the class. In order to be able to set this property once and all objects created further, already have this property set.

Linux ARM64

class gpio {
    public:

    // some object properties
    string board_name;
    int phy_gpio;

    // Default constructor
    gpio() {

    }

    // Parameterized Constructor
    gpio (int phy_pin_num) {
        ...
    }

    // method specifying the board_name property
    void set_board (string board) {
        board_name = board;
    }

    privare:

};

The problem is that the gpio port number depends on the model of the board. It is necessary to make the board model set once, and then used to create objects of the gpio class.

The name of the board must be set before creating the gpio object, that is, before creating any of the available class objects using any of the available constructors.


Solution

  • have you looked the static keyword ? If I understood it right, after you initialize your first GPIO, you can set its model and every other gpio should have this same value.

    // Example program
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class gpio {
       public:
    
       static string board_model;
       gpio() {}
       ~gpio() {}
       string returnModel() {return board_model}
    };
    
    string gpio::board_model = "none";
    
    int main()  {
    
        gpio pin;
        std::cout << pin.returnModel() << std::endl;
        return 0;
    }