0

I have see codes' example such as ::function() in VS C++ but I cannot uniderstand what does it means.

I understand that :: is used for accessing the member function from outside class, but I have seen examples where I do not find this case. For example in following code

// For painting in memory
class MemCanvas: public Canvas
{
public:
    MemCanvas (HDC hdc) 
        : Canvas (::CreateCompatibleDC (hdc))
    {}

    ~MemCanvas ()
    {
        ::DeleteDC(_hdc); 
    }
};

See the ::CreateCompatibleDC() function !! I understand that it is defined in the Windows.h but how it is defined here I cannot guess.

Sjon
  • 4,989
  • 6
  • 28
  • 46
SJa
  • 487
  • 4
  • 14
  • 7
    Unary `::` refers to the global namespace. – chris Feb 24 '14 at 05:19
  • 2
    `NamespaceName::X` and `ClassName::X` are used for accessing a member of a namespace or class. `::X` is used for accessing a member of a global namespace (the nameless namespace that everything at file scope belongs to, unless explicitly placed inside some named namespace or class) – Igor Tandetnik Feb 24 '14 at 05:20
  • 1
    The question was about classes rather than functions, but you might still find my answer [here](http://stackoverflow.com/questions/4269034/what-is-the-meaning-of-prepended-double-colon-to-class-name/4269232#4269232) useful. – Tony Delroy Feb 24 '14 at 05:32
  • 1
    The code example provided is not complete, hence assuming some facts. ::CreateCompatibleDC() is defined and declared at some includes (windows.h) that might be mentioned in header file of class in use. As mentioned by @chris "::" refers to global namespace. So basically you are searching for CreateCompatibleDC in global namespace and not current or some other namespace. Note ::CreateCompatibleDC() is not defined here as use say in your question, its just called from your initialization parameter list. Base class Canavas Constructor is using the outcome of ::CreateCompatibleDC() as its parametr – Nik Feb 24 '14 at 05:33
  • 1
    You seem to understand what `::` means, so what exactly don't you understand about this code? `::CreateCompatibleDC()` calls the function with that name that was declared in the global namespace; the result of that function call is passed to the `Canvas` constructor. – Mike Seymour Feb 24 '14 at 05:42
  • now I understand this...thanks ^^ Actually I did not properly learn the OOP based C++, but I am and FPGA designer and now doing OOP hence finding things a little hectic in beginning ^^ – SJa Feb 24 '14 at 05:54

1 Answers1

1

:: refers to the global namespace.

As a prefix for a function call it indicates that this is not a member function, it's a global namespace function.

Together with the naming convention for Windows API functions, it pretty much identifies a Windows API function as such. To the reader. Usually there's no naming conflict so it's not necessary for the compiler: it's just a device to communicate to a reader of the code.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331