Difference between ->, :: and . Operators?

I’m still a bit new to C++ and just need this cleared up.

2 Likes

Hi,

"->" is pointer to a reference within memory.

MyClass* myClass=new MyClass();

myClass->myFunc();

points to myfunc on class instance myClass


"::" is used for access static methods or members

MyClass::MyFunc() 


class MyClass {

    static void MyFunc();

}

"." is access to non dynamic instanced data like structures

FMyStruct struct;
struct.a
struct.b

UObject myObject;
myObject.getDisplayName();

(Just a quick overview on usage, probably not complete)

-freakxnet

It’s probably a good idea to get more familiar with the basics of C++ before jumping straight into something as complex as the Unreal Engine. Luckily, that’s easier than ever these days. I’d recommend reading through some of the material here first: http://www.learncpp.com/

Small correction “::” is namespace marker, same as in Java and C# you use “.” for that in C++ it is “::”.

Because of possible name conflicts between classes, when you refrence to static members of class and structs you also need to use class namespace so compiler knows from which class that member is. The same reason you use it when you define function code in cpp file you put class name at function name.

Namespaces are not used much in UE4 but if you use C++ standard library (which you can in UE4, but it’s not recommanded, only if you really need it) you would use “std” namespace on all there functions and it’s not because they are in class they simply under that namespace

“.” is to access variable it self (like pointer, which in most cases is useless wither way as pointer is not structure and don’t have members)

“->” is to access what is variable pointing to

Im saying that because in UE4 “->” and “.” has extra sense… well actually this is how you should think about them in C++, as C++ allows to override “->” operator (but ofcorse code dont need to follow this rule, but in UE4 it do). In UE4 you can meet TSharePtr and TShareRef which by it self has functions on there own (like IsValid() to check if object they pointing to still exists) which you use “.” to call them, but there role is similar of the pointer and you access object they pointing to you use “->”.

Thank you for mentioning TSharePtr and TShareRef :).