Edit: See SAKryukov's answer for it's usage in the case you posted. Read the below if you want to know about how else it's used.
:: it is used in combination with namespaces or to solve name conflicts.
Consider this:
class A
{
public:
void PrintText() { std::cout << "A"; }
};
class B : public A
{
public:
void PrintText() { std::cout << "B"; }
};
int _tmain(int argc, _TCHAR* argv[])
{
B b = b;
b.PrintText();
b.A::PrintText();
return 0;
}
First of you see
std::cout
this tells the compiler to look for cout in the std namespace.
then when we call
b.PrintText()
"B" is printed but, if we wanted to use
A.PrintText
we do it like this
b.A::PrintText()
so we tell the compiler that we want to use
A
's implementation of
PrintText()
.
A more common problem is when you use multiple inheritance
class Horse
{
public:
void Eat() { }
void Run();
};
class Bird
{
public:
void Eat() { }
void Fly();
};
class Pegasus : public Horse, public Bird
{
public:
void Eat() { Horse::Eat(); }
};
int _tmain(int argc, _TCHAR* argv[])
{
Pegasus pegasus;
pegasus.Eat();
return 0;
}
A horseat leastbird eat different things and in diffPegasusays (atleast looks different). So here we have a pegasus it's a Horse and a Bird (it has four legs, can run anddifferentngs and can fly) but as horses aPegasuss eats in firrent ways we need to decide how the pegasus eats, here we have decided that it eats like a horse
void Eat() { Horse::Eat(); }
.
I hope it's clear, otherwise tell me and I'll try to clear up any parts that you don't understand.