arrow operator c++

/* 
 the arrow operator is used for accessing members (fields or methods)
 of a class or struct
 
 it dereferences the type, and then performs an element selection (dot) operation
*/

#include <iostream>
using std::cout;

class Entity {
public:
	const char* name = nullptr;
private:
	int x, y;
public:
	Entity(int x, int y, const char* name)
		: x(x), y(y), name(name) {
		printEntityPosition(this); // "this" just means a pointer to the current Entity
	}

	int getX() { return x; }
	int getY() { return y; }

	friend void printEntityPosition(Entity* e);

};

// accessing methods using arrow
void printEntityPosition(Entity* e) {
	cout << "Position: " << e->getX() << ", " << e->getY() << "\n";
}

int main() {
	/* ----- ARROW ----- */

	Entity* pointer = new Entity(1, 1, "Fred");
	//printEntityPosition(pointer); redacted for redundancy (say that 5 times fast)
	
  	cout << (*pointer).name << "\n"; // behind the scenes
	cout << pointer->name << "\n"; // print the name (with an arrow)

	/* ----- NOT ARROW ----- */

	Entity not_a_pointer(2, 2, "Derf");
	//printEntityPosition(&not_a_pointer); & to convert to pointer

	cout << not_a_pointer.name << "\n"; // print the name (with a dot)

	/* ----- LITERALLY NEITHER ----- */

	std::cin.get(); // wait for input
	return 0; // exit program
}


Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
cpp when should i use arrow what does arrow mean in c++ when is arrow used in cpp arrow syntax in c++ why do we use arrow with this &quot;this-&gt;&quot; in c++ classes inline arrow function c++ cpp arrow operator after function c++ arrow notation c++ arrow function C++ arrow operator in class when do we use the arrow operator in c++ pointers arrow function cpp cpp arrow alternative to arrow operator in cpp arrow operator c++ no arguments arrow syntax with reference c++ how to code a arrow in c++ what do arrows do in c++ arrow sign in c++ arrow syntax c++ arrow using symbol in c++ arrow notation c++ c++ arrow equivalent arrow functions in c++ arrow function c++ this and arrow in class cpp arrows in c++ c++ arrow operator return what does the right arrow mean in c++ . dot vs -&gt; in c++ *. vs -&gt; in c++ pointers c++ -&gt; nad point c++ what does -&gt; mean -&gt; operator cpp arrow or dot c++ dot vs arrow cpp dot vs arrow in cpp arrow functions c++ -&gt; cpp arrow operator in class c++ dot and arroe c++ this and arrow operator arrows in cpp c++ []() -&gt; C++ arrow operator get field of pointer value -&gt; in cpp c++ arrows c++ -&gt; operation dot membership operator structure pointer c++ oeprator what -&gt; sign dipicts in pointer in cpp C++ arrow operator access field of pointer arrow operator c++ class c++ dot vs arrow this-&gt; notation c++ dot vs arrow c++ what does the -&gt; operator do in c++ what does the arrow mean in c++ c++ this arrow dot and arrow operator in c++ -&gt; versus . c++ -&gt; vs . in c++ c++ . vs -&gt; when to use . and when -&gt; in c++ what is -&gt; operator in c++ arrow operator in cpp what si -&gt; c++ arrow operator in c++ can arrow operator access variables arrow operatro c++ c++ arrow operator referencer arrow in c++ c++ -&gt; equal vs arrow in c++ arrow vs equal operator in c++ c++ . vs arrow arrow operator. in c++ implement dereference operator and arrow operator c++ access with arrow in c++ -&gt;sign in c++ what is the arrow operator called c++ What is the arrow operator called? c++ -&gt; operator C++ what does the -&gt; does in c++ -&gt; operator in c++ -&gt; in c++ -&gt; c++ arrow operator c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source