-6

I'm just curious whats the difference of :: and -> in C++?

Currently studying C++ because I want to study openGL and lots of tutorial in openGL using c++ so I'll just go with language that has many tutorials :)

In java or C# if you want to call a function or a reserved function you just use "." like for example text1.getText(); If you are to convert it to C++ would that be text1->getText()? and what to call them? Title is not appropriate. If -> is equal to "." in java then whats the use of "::"? I believe there are many questions out there like mine but I don't know what to call them so I cant have an accurate information. By the way I found this :: think while using sfml.

Here is an example

if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
            }

void renderingThread(sf::Window* window)
    {
        // activate the window's context
        window->setActive(true);


        // the rendering loop
        while (window->isOpen())
        {
            // draw...

            // end the current frame -- this is a rendering function 

(it requires the context to be active)
                window->display();
            }
        }

window uses -> while sf uses ::

eddie
  • 1,252
  • 3
  • 15
  • 20
CodeAndWave
  • 1,586
  • 3
  • 23
  • 33
  • `::` is how you get to a static function or field (which don't require an instance of an object), `->` is dereferencing a pointer to get to a function or field (and thus requires an instance, and uses the instance's specific values). Look up C++ operators – Patashu May 27 '13 at 12:17
  • Use `::` to access members by class name, `.` to access by object value, `->` by object pointer. – riv May 27 '13 at 12:19
  • 1
    No one's explicitly mentioned that the scope resolution operator works for non-static members as well. `auto f = std::bind(&Foo::bar, fooInstance);` – chris May 27 '13 at 12:20
  • Okay. Thanks guys :). -6 votes :( too bad – CodeAndWave May 27 '13 at 12:49
  • Please get yourself a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo May 27 '13 at 13:44
  • Cool great collection. Thanks – CodeAndWave May 27 '13 at 14:36
  • please read a useful book instead. – Raindrop7 Jan 28 '17 at 22:43

2 Answers2

11

:: is the scope resolution operator, used for referencing static class members and namespace elements.

-> is the indirect reference operator, used for referencing members methods and fields on an instance pointer.

. is the direct reference operator, used for referencing member methods and fields on an instance.

Since Java has no real pointers, it has no use for the indirect reference operator.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
4

The operator -> is used when you have a pointer to an object and dereference the pointer, i.e.

string* str = new string("Hello, World");
const char* cstr = str->c_str();

whereas

string str("Hello World");
const char* cstr = str.c_str();

is used to directly reference a member.

The :: is the "scope-operator". You can use it to call static members of a class or reference a member in a namespace.

namespace X{
 int a;
}

int main() {
...
X::a = 4;
...
}

See Wikipedia on this.

bash.d
  • 13,029
  • 3
  • 29
  • 42