What does "::" do?

I am trying to decipher some example code and the following line appears in a local .h file:

void M5PaperTouchAreas::loop() {

What is the purpose of the "::" generally and specifically what do they do in the name of a function as here?

https://cplusplus.com/forum/beginner/111502/

1 Like

The "::" (double colon) is known as Scope Resolution Operator. You can try to find the meaning/purpose of this operator from the following sketch which contains class-based codes to blink the on-board LED (L) of UNO at 1-sec interval?

class DigitalIo  //Class Name (DigitalIo); do Capitalize the first letter for Class Name
{
  private:
    int ledPin;  //variable can only be accessed by the functions under public: specifier

  public:
    DigitalIo(int DPin);//: ledPin(powerpin) {} //inline constructor to initilize ledPin
    void ioDir();  //member function
    void ledOn();
    void ledOff();
};

DigitalIo led(13);  //led is called object of type DigitalIO

void setup()
{
  Serial.begin(9600);
  led.ioDir();     //setting up the direction of IO line(13) as output
}

void loop()
{
  led.ledOn();
  delay(1000);
  led.ledOff();
  delay(1000);
}

void DigitalIo::ioDir() //member function definition; :: (double colon) is calld scope resolution operator
{
  pinMode(ledPin, OUTPUT);
}

void DigitalIo::ledOn()
{
  digitalWrite(ledPin, HIGH);
}

void DigitalIo::ledOff()
{
  digitalWrite(ledPin, LOW);
}

DigitalIo::DigitalIo(int x)
{
  ledPin = x;
}

a class is normally defined in a ,h file which is included in other files using that class. the class definition does not need to define the functions of that class, i can just provide prototypes.

so the functions need to be defined in another .cpp files and need to be associated with the class they are in and are defined as class::function-name

1 Like

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