0

I am trying to do some hello world stuff after completing the standard c++ tutorial. The first thing I tried doing is drawing directly on the screen without a window. I found this and it works.

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HDC screenDC = ::GetDC(0);
    ::Rectangle(screenDC, 200, 200, 300, 300);

    int exit; cin >> exit;
    return 0;
}

enter image description here

But nowhere in the standard c++ tutorial does it cover anything like this

HDC screenDC = ::GetDC(0);
::Rectangle(screenDC, 800, 200, 300, 300);

What is Rectangle a member of?


If you don't mind I have some other questions that might be simple. If not don't worry about it.

Community
  • 1
  • 1
user1873073
  • 3,580
  • 5
  • 46
  • 81
  • 5
    By the way, that C++ tutorial is in no way "standard". – Joseph Mansfield May 14 '13 at 17:21
  • 2
    That is the *scope resolution operator*. In this case, `::Rectangle` means that the function `Rectangle` lives in the global namespace – Andy Prowl May 14 '13 at 17:21
  • 1
    possible duplicate of [What is the meaning of prepended double colon "::" to class name?](http://stackoverflow.com/questions/4269034/what-is-the-meaning-of-prepended-double-colon-to-class-name) – Moo-Juice May 14 '13 at 17:24
  • It's a much better idea to have a topmost, fullscreen, colour key window instead of drawing on the screen. Use `CreateSolidBrush` and select it into the DC or something to change colour (there's a lot of info on GDI on MSDN), and Petzold's book is wonderful. – chris May 14 '13 at 17:27
  • Please don't add additional questions not relevant to the original question - create new questions for these items. – Moo-Juice May 14 '13 at 17:27

4 Answers4

9

:: is the scope resolution operator.

scope::name means use the name declared in the class or namespace called scope.

::name means use the name declared in the global namespace. Usually, it's optional; but sometimes you need it if there's something else with the same name in the current scope.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Specifically, `::` is the [scope resolution operator](http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm). – Caleb May 14 '13 at 17:25
  • @Caleb: Yeah, I guess I should mention that. I assumed the OP knew what it was (since he asked "What is Rectangle a member of?"), but completeness is good. – Mike Seymour May 14 '13 at 17:27
7

:: like that accesses the global namespace. It is namespace resolution. You can just drop those :: if you like, they are extra cruff.

RandyGaul
  • 1,915
  • 14
  • 21
  • 1
    You can drop them here; but sometimes you need them to resolve ambiguities. – Mike Seymour May 14 '13 at 17:25
  • Well, you can drop them in *this case*. But if you're currently in a different namespace, which also has something with the same name, just writing the symbol name will be ambiguous. – leemes May 14 '13 at 17:25
2

:: is the scope resolution operator. ::Identifier will refer to anything in the global namespace. It is not always necessary however. It's only really needed when not using it will cause an ambiguity:

int i = 0;

void func() {
    int i = 0;

    ::i = 1; // This will set the global i
    i = 1; // This will set the local i
}

(Note: I do not endorse globals in any way)

user123
  • 8,970
  • 2
  • 31
  • 52
1

:: is used to access things that are in classes or namespaces. (Or in this case, that are not.)

So if you have a class Foo with a static method bar you can call it like this Foo::Bar();

Also, if you have a namespace MyLibrary with a function PrintPrettyThings you can call it like this: MyLibrary::PrintPrettyThings();

And if you have another function Bar somewhere and you are inside a method of Foo you can use ::Bar(); to call the Bar that is outside of Foo, otherwise you will just call Foo::Bar.

Sarien
  • 6,647
  • 6
  • 35
  • 55