Global Scope and scope resolution operator

Are there any situations to explicitly use the scope resolution operator with global scope? I can imagine a situation like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   //Please ignore the nonsense from a design point of view
#include <cmath>

class IntWrapper{
   public:
      IntWrapper& pow(char);
      IntWrapper(char);
   private:
      int m_int;
};
//...
IntWrapper::IntWrapper(char toconvert):
   m_int(IntWrapper::pow(toconvert))
{
   m_int = ::pow(m_int,100);
}
//Implementation of IntWrapper::pow(char)... 


But then I would think that the writer should have used a different name, and that using the scope resolution operator in the constructor body is still pointless...
Last edited on
The header <cmath> is not required to declare ::pow, only ::std::pow (which most people refer to as std::pow, but since we're talking about ::..

I see ::new used occasionally.
Oh, sorry. I probably wrote that tidbit really poorly because I was focusing on operator::.

Also, can you post an example of ::new being used?
Last edited on
What do you mean by operator::? The way you write it, it sounds like you are going to overload it (which you can't AFAIK).

You can override the ::operator new() or call it directly. It can be used just like malloc() (and frequently is coded to call malloc()). However, calling ::operator new() allows for it to be overridden unlike malloc(). Default operator new() for classes call ::operator new().
I only called it "operator::" because I thought all operators' function names were preceded by "operator," whether they were overload-able or not...

So, by specifically using ::operator new(), you can do this?
1
2
3
4
5
6
7
8
9
10
class Super{
   public:
      virtual ::operator new();
   private:
      int* baz;
};

class Sub: public Super{
   ::operator new();
};


Sorry if I misunderstood what you said. I'm a visual learner, so I kind of need examples to help me.
Even if you would write

::pow(m_int,100);

there is an ambiguity because function pow is overloaded for floating numbers.

The correct code (without testing :) ) will be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   //Please ignore the nonsense from a design point of view
#include <cmath>

class IntWrapper{
   public:
      IntWrapper& pow(char);
      IntWrapper(char);
   private:
      int m_int;
};
//...
IntWrapper::IntWrapper(char toconvert):
   m_int(pow(toconvert))
{
   m_int = ::pow((double )m_int,100.0);
}
//Implementation of IntWrapper::pow(char)...  
Thanks for the clarification. vlad. By the way, can you give an example on using ::operator new() ? Or an example where ::foo() is necessary?
By the way, std::pow is overloaded for integers (as of C++11, earlier in some compilers), only ::pow isn't.. which is another difference.

can you give an example on using ::operator new() ?

Effective C++ has a classic example:
1
2
3
4
5
6
7
8
void * Base::operator new(size_t size)
{
    if (size != sizeof(Base)) // if size is "wrong,"
    return ::operator new(size); // have standard operator
                                 // new handle the request
    ... // otherwise handle
        // the request here
}

It's in Item 8.


an example where ::foo() is necessary?

Every once in a while when I feel lazy, I write a container toupper as
1
2
3
#include <ctype.h> // not cctype!
...
std::transform(container.begin(), container.end(), container.begin(), ::toupper);

It's a little silly, but it's an example.
Last edited on
Ah, thanks for those examples. I tried figuring out an example from what AdrianH said, but everything I came up with seemed wrong or needlessly complex. Now that you posted that example with ::operator new(), I feel dumb for not having thought of that.

Just one last question: how common is ::foo in serious programs, including ::operator new()?
Topic archived. No new replies allowed.