3

I was reading the book C++ Primer by Stanley B. Lippman and in the section of Variables and Basic Types I saw the scope operator ::.

I had already read a little about the operator overload and I think it could be very useful in special cases, but when I searched in the internet I found out that I simply cannot overload the :: operator.

In this post I found that the . operator can be overload. However, this can lead to questions about whether an operation is meant for the object overloading . or an object referred to by ..

Thus, I think that maybe there is a way to overload the ::.

But if it can't, can anyone explain me why?

An example of my idea for the :: operator:

#include <iostream>

/*
 *For example:
 *I wanna increase 1 unit every time 
 *I call the global variable r doing ::r
 *insede of the main function
 */

int r = 42; 

int main()
{
    int r = 0;
    std::cout << ::r << " " << r << std::endl; //It would print 43 0 after the operator overload

    return 0;
}
Community
  • 1
  • 1
Gabriel
  • 763
  • 1
  • 10
  • 28
  • 7
    The scope "operator" unlike all the operators does nothing at run time, it affects name lookup at compile time, that's why you can't overload it. – Borgleader Aug 19 '15 at 19:27
  • @Borgleader and I cannot make it do something at run time? – Gabriel Aug 19 '15 at 19:30
  • @GabrielMello No because its only job is to tell the compiler where to find names. – Borgleader Aug 19 '15 at 19:34
  • 7
    _"I found that the . operator can be overload"_ - No, the `.` operator _cannot_ be overloaded. – Captain Obvlious Aug 19 '15 at 19:37
  • @CaptainObvlious but if you see that question the user tells "can in principle be overloaded using the same technique as used for ->." – Gabriel Aug 19 '15 at 19:38
  • @Borgleader Oh it explains a lot. Thank you for that! So if I have two variables with the same name, one local and another global, I was thinking about use ::variable_name to call the global variable inside the function and already do an operation with it, but it wouldn't be possible, right? – Gabriel Aug 19 '15 at 19:43
  • @GabrielMello [Like this?](http://coliru.stacked-crooked.com/a/aed0403edb1e8b44) – Borgleader Aug 19 '15 at 19:51
  • You are misinterpreting that portion of the post. In principle yes you _could_ overload the `.` operator _if_ the language supported it. C++ does _not_ support overloading the `.` operator. – Captain Obvlious Aug 19 '15 at 19:52
  • See http://stroustrup.com/bs_faq2.html#overload-dot – dyp Aug 19 '15 at 19:55
  • @Borgleader almost, but without the necessity of ::s = "Hello ";. For example, I would do this with numbers, so everytime I call the global variable I would already increment 1 unit to it. In your code would be change the "Blah" to "Hello" without make it explicit inside of your function. Can you understand? – Gabriel Aug 19 '15 at 19:59
  • @GabrielMello How would you refer to the local variable if it picks the global one without explicitly doing ::? I dont think what you want makes sense. It would make things ambiguous and/or very confusing. – Borgleader Aug 19 '15 at 20:02

2 Answers2

9

You cannot overload it.

The scope "operator" unlike all the operators does nothing at run time, it affects the name lookup at compile time and you cannot change it since its job it is only to tell the compiler where to find names.

That's why you can't overload it.

For example:

#include <iostream>
#include <string>

std::string s = "Blah";

int main()
{
    std::string s = "World";
    ::s = "Hello ";

    std::cout << ::s << s << std::endl;

    return 0;
}

See on Coliru

Gabriel
  • 763
  • 1
  • 10
  • 28
Borgleader
  • 15,826
  • 5
  • 46
  • 62
8

The reason you cannot overload :: is the standard forbids it. Section 13.5.3 has

The following operators cannot be overloaded:

. .* :: ?:

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I already know this, but I'm asking why. Please, take some time to read my question again, thank you. – Gabriel Aug 19 '15 at 19:44
  • 4
    @GabrielMello you asked `But if it can't, can anyone explain me why` And I am showing the reason you can't is because the standard says so. – NathanOliver Aug 19 '15 at 19:53
  • If you want to know _why_ the Standard says so, there's an old book (Design & Evolution of C++). But this decision is more than 2 decades old. – MSalters Aug 20 '15 at 09:49