How does the scope operator actually work?

Hello!
When I started to learn the SFML library I encountered a lot of usage of the scope operator particularly with sf.That's when I thought that's a namespace.After adding namespace sf there were two scopes left and they can't be removed because it will give a compilation error.And neither is a namespace.
My questions are.
Why do we need to use the scope operator if we are not using a namespace,isn't it used for accessing global variables and class functions for definitons?
And why is there a scope in front of Color and Event in the folowing program,and why do i need to use the scope operator for them?

Thank you for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
    RenderWindow window(VideoMode(200, 200), "SFML works!");
    CircleShape shape(100.f);
    shape.setFillColor(Color::Green);

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}
It is the scope resolution operator. You need to use it to access anything declared within its namespace (or a class, in some cases).

using namespace sf; essentially makes everything declared in the sf namespace visible within the global namespace.

As for Color::Green, the sf::Color class has static members declared as static const Color Green; (and more for other colors). You can see the documentation for that here:
https://www.sfml-dev.org/documentation/2.4.2/Color_8hpp_source.php

As for Event::Closed, it is an enum. sf::Event has an enum called EventType visible within the Event class, with one item called Closed[/code[, but you can access it like [code]sf::Event::EventType::Closed or like sf::Event::Closed.
You can see the documentation for that here:
https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Event.php#af41fa9ed45c02449030699f671331d4a

using namespace sf; isn't sufficient to see Color::Green or Event::Closed.
You can think of it like nests. The true nesting is sf::Color::Green and sf::Event::Closed. using namespace sf; only peels off sf.

I generally advise against using using namespace.
I see.
I do remember seeing enum used with scope,which is also one of my first times seeing this in practice.
as for the static part I don't know how it works can you reccomend a page to read about it?
And if there is static is there a Heap keyword?
And yes I know that using namespace is bad but from what I gather it's for big projects.
And for now I don't think I'll be getting anywhere near to big projects.
Still thank you I really appreciate it.
I see thank you
And for now I don't think I'll be getting anywhere near to big projects.

People prefer to do things "like I've always done them". The Force of Habit is strong in them.

I have learned that if I don't do things right already today, then the next opportunity will be a while after the end of the Universe.
Last edited on
If you're saying that this is a habbit of mine.I don't think so.Though it could be me and I did say that i haven't been in big projects nor would I get close to them.
Perhaps I need to be in a big project in order to see if this is a habbit or not.
Still I don't think I'm good enough or ready for a big project as I don't even know what static,recursion is and probably more.
I am thankful that you're trying to help me.
But I'm very slow and that's why I think that I won't be any big projects soon.
Last edited on
Topic archived. No new replies allowed.