Hello!

I am probably just going mad but. This code:

using namespace	System.Windows.Forms;

Generates the following error

1>GUItest.cpp(6): error C2143: syntax error : missing ';' before '.'
1>GUItest.cpp(6): error C2059: syntax error : '.'

..And I haven't the foggies why. Seems I cannot use .'s when declaring a namespace..

C++ uses the :: operator for scope resolution.

using namespace System::Windows::Forms;

There we go - thanks.

I am a bit confused about this "scope resolution" thing. A google search reveals that it is normally used to access "hidden" members etc., like when you declare global and local functions / variables with the same name.

However, in the context of declaring a namespace, what is the logic behind using scope resolution? What is its true definition?

I have also seen it being used to access interface member functions etc.

> A google search reveals that it is normally used to access "hidden" members etc.
Not really. The common use is linking names to a namespace either in definition or use:

class Foo {
public:
    void method();
};

// Scope resolution in name definition
void Foo::method()
{
    // ...
}
#include <iostream>

int main()
{
    // Scope resolution in name use
    std::cout << "hello world" << std::endl;
}

the :: symbol is known as the Scope Resolution operator because it is used to specify the scope in which an identifier resides within your program;

The concept of 'scope' generally applies anywhere that curly brackets can be found { } - the reason for the concept of scope is to allow the same identifier to exist within different logical partitions of your code. A namespace is one such type of partition, a class/struct is another. for example:

int num;

namespace foo
{
    int num;

    struct bar
    {
        static int num;
    };

    int bar::num;
};

int main()
{
    ::num = 1;
    foo::num = 2;
    foo::bar::num = 3;
}

if you read right-to-left, you could translate 'scope resolution' to 'exists within'. names which are not qualified with the scope resolution operator are assumed to be already declared within the current scope


Note that you may also have a block which exists in an unnamed scope, e.g. inside a function

int num;

void func()
{
    int num;
    {
        int num;
        {
             int num;
        }
    }
}

For obvious reasons, it is impossible for the scope resolution operator to qualify names within an unnamed scope; if the scope resolution operator is used without a name, it is assumed to be referring to the global scope. Hence why some coding standards documents insist that global variables must be fully-qualified using the :: symbol, to make them visually stand out against local variables.

commented: Nice explanations +4

@Bench & Radical Edvard

Thanks for the very thorough explanation! I think I'm getting the general idea now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.