C++ Tutorials

C++ Tutorials

I’ve been studying C++ for awhile but i still can’t grasp the usage of arrows ->

The Arror(->) is for accessing some thing beyond a pointer.

#include <iostream>
#include <string>
#include <map>
int main() {
class sample
{
private:
int a;
public:
int b;
void init(int a)
{this->a = a;}
void display()
{cout<<"a: "<<a<<endl;}
};

Today we’re talking all about the arrow operator in C++ we’re going to talk about what the arrow operator actually does for both struct and class pointers as well as implement our own arrow operator to see.

Syntax:

(pointer_name)->(variable_name)

How it works so over here my source code of our basic entity class type now if I create this object normally as I probably would like this in full print I have no issues but if this entity object was actually a pointer.

So either it’s it was allocated on the heap or maybe I just had a pointer to it for some reason like this in order to call that print function.

I can’t actually just use pointer dot print like that because this is just a pointer it’s basically just a numerical value I can’t just called dot print on it.

What I have to actually do is dereference that pointer and that can be done like so I can just say entity reference entity for example and then use the asterisk in front of the point like this to dereference it.

And then just substitute this with entity and my card works now to avoid this extra line what I could also do is use my pointer but surrounded with parentheses and dereference it like.

So now I can’t just write code like this because of operator precedence it’ll actually try and go to the object or print and then dereference the result of print that’s obviously not going to work.

So you have you have to actually do the dereferencing first and then call dot print now this this is okay and it works fine.

But it looks a little bit clunky so what we can do instead is just use the arrow operator instead of dereferencing the pointer and then calling dot print we can substitute all of that with just an arrow to print like this.

And what this actually does is dereference that entity pointer into it just an all entity type and then calls print so that’s pretty much all there is to it it’s just a shortcut for having it for us having to manually dereference events around everything with parentheses.

And then call our function or our variable as well instead of doing all that we can just use an arrow worst variable as well if I had some variables over here I’ll just make public int X for example. I could also just you know access X through the arrow like this and then set it equal to whatever I wanted to like so okay cool.

So that’s pretty much the default use case for the arrow operator that’s probably how you’re using it 90% of the time however as an operator in C++ it is actually possible to overload it and use it in your own custom classes.

And I’ll show you an example of why you might want to do that and how you can do that over here, so suppose that I was writing some kind of smart pointer class to point up to keep it simple.

I’m just going to have it have an entity pointer when I construct this script pointer I’m going to take in an entity as a parameter here and then assign it to my object in the destructor.

I’m going to call delete entity or delete M ulchhhh and so now I’ve got a basically scope to point a class that will automatically delete my entity when it goes out of scope so I can use it like source code pointer entity equals new entity and that looks pretty good now.

I want to actually be able to call this print function or access this X variable so how do I do that well right now I can’t really like I can use dots but then like I could make either this public or maybe I could just have something that returns an entity pointer like yet object like this maybe that will return my object that just looks way too messy.

I want to be able to use it like a heap-allocated entity right I want to be able to use it as if I’d written code like this which would mean that I could just write well that and it would work fine.

I want to be able to just substitute this and have it kind of be used the same way well that’s where you can overload the arrow operator make it do that for you instead of get object.

I can write entity pointer operator hour with no parameters like this and then just return em all and you can see suddenly this compiles and will run just fine if I hit f5 there you go.

You can see that it’s calling my function and printing hello now in the case of this being Const you could also provide a constellation of this operator so I’ll copy and paste this have a return a Constanta T and Mark the operators Const like this and that will now return a Const version of this and of course.

I’ve marked this function as constic it wasn’t constituency I’m not able to call that function so the function has to be marked as Const over here as well and everything basically works as if this was just a Const pointer like that no difference but now of course since it is a scoped pointer I voted the deletion of this actual object pretty cool stuff.

So that’s how you can overload the arrow operator to function in your own classes it’s very powerful it’s very useful because you can see that you can start to kind of define your constructs and urine types in the language and automate things and it looks like normal code which is exactly what we want a lot of people will argue that.

That’s a bit confusing because yeah it might look like normal code but it’s not however I think that if you use it properly and if you’re sensible about it then this this is actually really useful and can help keep your code really clean.

So fun I’m going to show you one more way how we can actually use the arrow operator to get the offset of a certain member variable in memory so this is kind of like a little bonus segment.

I guess of this episode but it has to do with the arrow operator soil I’ll put it in let’s just say that we have a struct here maybe called vector 3 and we basically just have a 3 in front of back to float X Y Z like this.

Now suppose that I actually wanted to find out what the offset of this Y variable was in memory so we know that this struct is structured out of floats of course.

So it’s got float X Y and said each float is 4 bytes so the offset of X is going to be 0 it’s the first thing in the struct Y is going to be 4 because it’s 4 bytes into the struct and then finally set is going to be 8 bytes.

But what happens if I suddenly move this around then well the class is gonna work the same way but they’re going it’s going to have a different layout in memory.

So maybe I want to write something for myself that actually tells me the offset of each of these members and I can do something like that using the arrow operator.

So what I want to do is kind of access these variables but instead of from a valid memory address just from 0 so this is kind of hard to explain but if I’ll but I’ll show you what I mean I’m literally going to write 0 and then cost this into a vector 3 pointer like.

So and then use the arrow to access X and this is going to give me this is going to try and give me some kind of piece of invalid memory but what I’m going to do is actually take the memory address of that X.

So now what I’m doing is basically getting the offset of that X because I’m starting at 0 this could also be written as null pointed by the way and if I finally take that and just cost it to a regular integer and write offset over here.

I’ll print that and I’ll hit up five you can see it gives me zero so what I’ll do next is I’ll change this to be Y and check out what that looks like for that seems right.

And then I’ll change it to Zed and of course the value should be eight and you can see that it is so what we’ve done here is we’ve used the arrow operator to basically get the offset of a certain value in memory pretty cool stuff and this is actually very useful for, when you’re serializing data into like a stream of bytes.

And you want to figure out offsets of certain things and we’ll kind of get into this kind of exciting code when we start doing the graphics programming series.

Difference between Dot(.) and Arrow(->) operator:

  • The Dot(.) operator is used to normally access members of a structure or union.
  • The Arrow(->) operator exists to access the members of the structure or the unions using pointers.

C++ Arrow Operator in Class

Example
#include<iostream>
class A {
   public: int b;
   A() { b = 5; }
};
int main() {
   A a = A();
   A* x = &a;
   std::cout << "a.b = " << a.b << "\n";
   std::cout << "x->b = " << x->b << "\n";
   return 0;
}
Output
This will give the output −

5

Arrow operator overloading in C++

The dereferencing operator-> can be defined as a unary postfix operator. That is, given a class −

myClassIterator.operator->()->APublicMethodInMyClass()

Objects of class Ptr can be used to access members of class X in a very similar manner to the way pointers are used. For example −

void f(Ptr p ) {
   p->m = 10 ; // (p.operator->())->m = 10
}

Dot operator C++

Dot (.) operator is known as “Class Member Access Operator” in C++ programming language, it is used to access public members of a class. Public members contain data members (variables) and member functions (class methods) of a class. a dot (.) is actually an operator in C++ which is used for direct member selection via object name.

Get $100 to try DigitalOcean - Try for Free for 60 Days

X