Difference between ".", "::" and, "->"
Asked Answered
G

4

5

In c++ is there any difference between these 3 blocks of code:

MyClass->m_Integer // 1
MyClass::m_Integer // 2
MyClass.m_Integer  // 3
Grab answered 1/8, 2011 at 5:1 Comment(1)
Make sure that you have a good introductory C++ book. It is very difficult to learn the language--and learn to use it correctly--without a good book.Bula
A
11

The -> and . operators are way to access members of an instance of a class, and :: allows you to access static members of a class.

The difference between -> and . is that the arrow is for access through pointers to instances, where the dot is for access to values (non-pointers).

For example, let's say you have a class MyClass defined as:

class MyClass
{
public:
    static int someValue();
    int someOtherValue();
};

You would use those operators in the following situations:

MyClass *ptr = new MyClass;
MyClass value;

int arrowValue = ptr->someOtherValue();
int dotValue = value.someOtherValue();
int doubleColonValue = MyClass::someValue();

In Java, this would look like:

MyClass ref = new MyClass;

int dotValue = ref.someOtherValue();
int doubleColonValue = MyClass.someValue();
Apogamy answered 1/8, 2011 at 5:6 Comment(1)
You can also use the :: operator (called the scope resolution operator) to also refer to members of a namespace or a class.Egin
M
6

-> means MyClass is a pointer to the class and said pointer needs to be dereferenced in order to get to member m_Integer

:: is the scope or namespace operator. It means that m_Integer is either static or needs you need to identify specifically which scope or namespace m_Integer is within.

. means that m_Integer is being access directly (not through a pointer) from MyClass. It pretty much how you would access memebers from within Java as well and should be the one you are most familiar with.

Mebane answered 1/8, 2011 at 5:6 Comment(0)
H
6
  • a::b -- a is a namespace or a class (not an instance), and consequently b needs to be some static entity
  • a.b -- a is an instance of a class and b is its member
  • a->b -- same as (*a).b (a is a pointer to an instance of a class)
Hhd answered 1/8, 2011 at 5:8 Comment(0)
T
0

Along with the other answers you've gotten, it's worth noting that operator-> can be overloaded. You've already gotten some reasonable explanations of how the built-in version work, but an overloaded operator-> is somewhat unusual, so perhaps it's worth adding a bit about how it works.

The standard describes how operator-> is unusual fairly succinctly (§13.5.6):

An expression x->m is interpreted as (x.operator->())->m for a class object x of type T if T::operator->() exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).

This has a couple of implications:

  1. Even though -> looks like a binary operator, from a viewpoint of overloading, it's essentially a unary operator -- your overload will be a member function that takes no argument.
  2. Your overload has to return one of two types: either a (reference to) an instance of another class that also overloads operator->, or else a pointer to some object (of class or struct type).

Since you can return an object that itself overloads operator->, an expression of the form x->m can hide an arbitrary amount of complexity (an arbitrary number of functions being called). What you write as x->m could really expand out to x->y->z->a->b->...m.

Tana answered 1/8, 2011 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.