Apologies for a question that I assume is extremely basic.
I am having trouble finding out online the difference between the operator :: and . in C++
I have a few years experience with C# and Java, and am familiar with the concept of using . operator for member access.
Could anyone explain when these would be used and what the difference is?
Thanks for your time
The difference is the first is the scope resolution operator and the second is a member access syntax.
So,
::(scope resolution) can be used to access something further in a namespace like a nested class, or to access a static function. The.period operator will simply access any visible member of the class instance you’re using it on.Some examples:
Note that a static function or data member is one that belongs to the class itself, whether or not you have created any instances of that class. So, if I had a static variable in my class, and crated a thousand instances of that class, there’s only 1 instance of that static variable still. There would be 1000 instances of any other member that wasn’t static though, one per instance of the class.
One more interesting option for when you come to it 🙂 You’ll also see:
Dynamic memory can be a little more confusing if you haven’t learned it yet, so I won’t go into details. Just wanted you to know that you can access members with {
::or.or->} 🙂