unshakeable
unshakeable

Reputation: 31

What does "name::name" means in C++?

I would like someone to explain me the "name::name" syntax and how it is used on C++ programming. I have been looking through but I don't get it yet. Thanks for help.

Here is context code:

void UsbProSender::SendMessageHeader(byte label, int size) const {
    Serial.write(0x7E);
    Serial.write(label);
    Serial.write(size);
    Serial.write(size >> 8);
}

Upvotes: 2

Views: 3670

Answers (2)

Dmitriy Kachko
Dmitriy Kachko

Reputation: 2914

It is operator for scope resolution. Consider that code

class A { public: void f(){} };
class B { public: void f(){} };
class C : public A, public B {};

int main(int argc, char *argv[])
{
    C c;
    //   c.f(); //  ambiguous: which one of two f() is called?
    c.A::f(); // OK
    c.B::f(); // OK
    return 0;
}

Upvotes: 2

strcat
strcat

Reputation: 5622

:: is the scope resolution operator.

std::cout is the name cout in the namespace std.

std::vector::push_back is the push_back method of std::vector.

In your code example:

void UsbProSender::SendMessageHeader(byte label, int size) const {
    Serial.write(0x7E);
    Serial.write(label);
    Serial.write(size);
    Serial.write(size >> 8);
}

UsbProSender::SendMessageHeader is providing the definition for the SendMessageHeader method of the UsbProSender class.

Another (more complete) example:

class Bar {
    int foo(int i); // forward declaration
};

// the definition
int Bar::foo(int i) {
    return i;
}

Upvotes: 12

Related Questions