đŸ•ˇī¸ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 103 (from laksa179)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

â„šī¸ Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
2 months ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH2.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.scaler.com/topics/arrow-operator-in-cpp/
Last Crawled2026-02-18 17:00:04 (2 months ago)
First Indexed2022-11-21 18:00:16 (3 years ago)
HTTP Status Code200
Content
Meta TitleWhat is the Use of Arrow Operator -> in C++? - Scaler Topics
Meta DescriptionLearn about the use of the arrow operator in C++ with scaler topics.
Meta Canonicalnull
Boilerpipe Text
Before learning about the Arrow Operator -> in C++, let's learn about the basics of Operators in C++. An operator is a symbol that is used to give instructions to the compiler to perform a particular operation on given operands. Each operator has its pre-defined operation. Operators are the basic building blocks of any programming language. There are different kinds of operators in C++, such as Assignment Operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, and many others operators. Now, let's understand what is -> in C++ (Arrow Operator). Arrow operator ( -> ) in C++ also known as Class Member Access Operator is a combination of two different operators that is Minus operator ( - ) and greater than operator ( > ). It is used to access the public members of a class , structure , or members of union with the help of a pointer variable. There is a .(dot) operator in C++ that is also used to access the members of a class. But . (dot operator) accesses the member or variable directly means without using the pointers whereas instead of accessing members directly the arrow operator( -> ) in C++ uses a pointer to access them. So the advantage of the -> operator is that it can deal with both pointer and non-pointer access but the dot( . ) operator can only be used to access the members of a class. Arrow ( -> ) operator in C++ represents the same meaning which is done by the ( * ) asterisk operator and dot( . ) operator. Arrow operator can be written in a different way which is using the combination of two operators *. . For example, (*s).element is the same as s->element . The brackets (parenthesis) used while using the combination of ( *. ) operators show that the precedence of the dot( . ) operator is more than * . Let's understand the use of the Arrow operator -> in C++ using some practical programming examples. Syntax Let's first take a look at the syntax for using the arrow operator in C++ for accessing the members of a class, structure, etc. Here, we have used the arrow operator to access the class member variable represented by class_member_name through the object pointer which is represented by pointer. Explanation Let's implement a simple program to explain and demonstrate the use of the -> operator in C++. In the below example, we have declared a structure Person which contains two member variables. The first one is the name variable which is a character array that stores the name of the Person and the other one is the age variable which is of integer type. Code Output Explanation In the above example, after creating classes and objects we have called the main() method. In the main() method, we have created an object info of structure type that is Person type. Then we used the arrow operator ( -> ) to assign the 17 value to the member variable age. After assigning the value to the member, we then used the arrow operator ( -> ) to access the member and print its value as we can see in the output. Examples of Arrow Operators -> in C++ We will implement several examples of the usage of the -> operator in C++ to understand about the arrow operator in more depth. Example 1 In this example, we will demonstrate the usage of the arrow operator -> in C++ with classes. Here, member functions of the class are accessed by the pointer object using the -> operator. Code Output Explanation In the above example, we have first created a Student class that contains some private variables and public functions. The first function which is percentage calculates the total percentage by using the marks stored in the total_marks variable. Now, as we can see the total_marks variable is private so we can use the this keyword to refer to the private variable. Now for referring to the current object's private variable, the this keyword uses the arrow operator. Example 2 In this example, we will demonstrate the usage of the arrow operator -> in C++ with unions . Here, member variables of the union are accessed by the pointer object using the -> operator. Code Output Explanation In this example, we created a union called Student with some variables. In the main function, the object of the union student is created and initialized with a NULL value. Here, we have dynamically allocated memory to the created union object. Since the object contains two variables we can refer to those variables using the arrow operator. So we have used the object name along with the arrow operator to refer to the desired union variable. Example 3 In this example, we will demonstrate the usage of the arrow operator -> in C++ with struct . Here, member variables of the structure are accessed by the pointer object using the -> operator. Code Output Explanation In this example, we have created a structure called Employee having a variable name . In the main function, we have the object of the student structure and initialized them with the NULL value. Now, we have dynamically allocated the memory to the union object. Since the object contains one variable we can refer to that variable using the arrow operator. So we have used the object name along with the arrow operator to refer to the desired struct variable. Asterisk and Dot Operator Instead of Arrow Operator Let's see an example to understand the usage of the arrow operator and combination of ( * ) and ( . ) operators which is a kind of similar way to access the members of a class. Example Code Output Explanation In this example, we have created a class called Combi has a variable and member functions. In the main function, we have created the object of the Combi class. Now, we have dynamically allocated the memory to the object s using the new keyword. Since we have used the new keyword the object is created and a reference to the object(pointer) is returned. We can either use the arrow operator or the Asterisk( * ) and dot( . ) operator to refer to the data members and member functions. Here, first the *s is resolved and then the dot operator will help us to locate the actual data member and member functions. Now, we will use the arrow operator to implement the same program. This will also give the same output. Code Output Explanation In this example, we have created a class called Combi that has a variable and member functions. In the main function, we have created the object of the Combi class. Now, we have dynamically allocated the memory to the object s using the new keyword. Since we have used the new keyword the object is created and a reference to the object(pointer) is returned. Here, we have used the arrow operator to refer to the desired class variable or member function. Different Operators in C++ Operators are the basic building block of any programming language. An operator is a symbol that will be operated on a variable or a constant value. They are used to give instructions to the compiler to perform a particular operation. Each operator has its pre-defined operation. There exists a different kinds of operators in C++ which are listed below: C++ Arithmetic Operators Arithmetic operators are used to perform arithmetic operations on the operands. These operators include + (addition), - (Subtraction), * (Multiplication), % (Modulo), and / (Division). C++ Relational Operators Relational Operators are used to compare the values of two operands. These operators include == (Is Equal To), != (Not Equal To), > (Greater Than), < (Less Than), >= (Greater Than or Equal To), <= (Less Than or Equal To). C++ Logical Operators Logical Operators are used to find whether an expression is true or not. These operators include && (Logical AND), || (Logical OR), and ! (Logical NOT). C++ Bitwise Operators Bitwise operators are used to performing operations on integers at the bit level. These operators include & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), << (Bitwise Shift Left), >> (Bitwise Shift Right). C++ Assignment Operators Assignment operators are used for assigning values to a variable. These operators include = , += , -= , *= , /= , %= . and other operators. To learn more about Operators in C++ , refer to the article Operators in C++ Conclusion An operator is a symbol that is used to give instructions to the compiler to perform a particular operation on operands. Arrow operator ( -> ) in C++ also known as Class Member Access Operator is a combination of two different operators that is Minus operator (-) and greater than operator (>) . It is used to access the members of a class , structure , or members of union with the help of a pointer variable. There is a .(dot) operator in C++ that is also used to access the members of a class. . (dot operator) accesses the member or variable directly Whereas instead of accessing the members directly the arrow operator( -> ) in C++ uses a pointer to access them. Arrow ( -> ) operator in C++ represents the same meaning which is done by the ( * ) asterisk operator and dot( . ) operator. The combination of ( * ) and the ( . ) operator can also be used to access the members of a class similarly.
Markdown
[Experience![Scaler](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/scalerLogoWhite.svg)]() [![Scaler](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/scalerLogoWhite.svg)](https://www.scaler.com/?utm_source=topics) [Academy](https://www.scaler.com/academy/?utm_source=topics) [Data Science](https://www.scaler.com/data-science-course/?utm_source=topics) [AI/ML](https://www.scaler.com/ai-machine-learning-course/?utm_source=topics) [DevOps](https://www.scaler.com/devops-course/?utm_source=topics) [Neovarsity](https://www.scaler.com/neovarsity/?utm_source=topics) [![Scaler Topics Logo](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/topic_logo.svg)![Scaler Topics Logo](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/ScalerTopics_Logo.svg)](https://www.scaler.com/topics/) [Topics]() [Explore]() [New Skill Test]() [Courses]() [Free Masterclass](https://www.scaler.com/topics/events/) Search for Articles, Topics [Experience![Scaler](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/scalerLogoWhite.svg)]() [Experience Scaler]() What is the Use of Arrow Operator -\> in C++? What is the Use of Arrow Operator -\> in C++? [C++ Cheatsheet]() # What is the Use of Arrow Operator -\> in C++? By Mohit Sahay 6 mins read Last updated: 23 Apr 2024 335 views Learn via video course ![](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/free.svg)FREE [View all courses](https://www.scaler.com/topics/courses/) ![C++ Course: Learn the Essentials](https://www.scaler.com/topics/images/instructor-prateek.webp) C++ Course: Learn the Essentials by Prateek Narang ![](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/user-circle-check.svg) 1000 5 [Start Learning](https://www.scaler.com/topics/course/cpp-beginners/) [Start Learning](https://www.scaler.com/topics/course/cpp-beginners/) [View all courses](https://www.scaler.com/topics/courses/) ![C++ Course: Learn the Essentials](https://www.scaler.com/topics/images/instructor-prateek.webp) C++ Course: Learn the Essentials by Prateek Narang 1000 5 [Start Learning](https://www.scaler.com/topics/course/cpp-beginners/) [Topics Covered]() Before learning about the **Arrow Operator -\>** in C++, let's learn about the basics of Operators in C++. An **operator** is a symbol that is used to give instructions to the compiler to perform a particular operation on given operands. Each operator has its pre-defined operation. Operators are the basic building blocks of any programming language. There are different kinds of operators in C++, such as Assignment Operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, and many others operators. Now, let's understand what is -\> in C++ (Arrow Operator). **Arrow operator** (\-\>) in C++ also known as Class Member Access Operator is a combination of two different operators that is Minus operator (\-) and greater than operator (\>). It is used to access the public members of a **class**, **structure**, or members of **union** with the help of a pointer variable. There is a .(dot) operator in C++ that is also used to access the members of a class. But .(dot operator) accesses the member or variable directly means without using the pointers whereas instead of accessing members directly the arrow operator(\-\>) in C++ uses a pointer to access them. So the advantage of the \-\> operator is that it can deal with both pointer and non-pointer access but the dot(.) operator can only be used to access the members of a class. Arrow (\-\>) operator in C++ represents the same meaning which is done by the (\*) asterisk operator and dot(.) operator. Arrow operator can be written in a different way which is using the combination of two operators \*.. For example, (\*s).element is the same as s-\>element. The brackets (parenthesis) used while using the combination of (\*.) operators show that the precedence of the dot(.) operator is more than \*. Let's understand the use of the Arrow operator \-\> in C++ using some practical programming examples. ### Syntax Let's first take a look at the syntax for using the arrow operator in C++ for accessing the members of a class, structure, etc. ``` ``` Here, we have used the arrow operator to access the class member variable represented by class\_member\_name through the object pointer which is represented by pointer. ### Explanation Let's implement a simple program to explain and demonstrate the use of the \-\> operator in C++. In the below example, we have declared a **structure** Person which contains two member variables. The first one is the name variable which is a character array that stores the name of the Person and the other one is the age variable which is of integer type. **Code** ``` ``` **Output** ``` ``` **Explanation** In the above example, after creating classes and objects we have called the main() method. In the main() method, we have created an object info of structure type that is Person type. Then we used the arrow operator (\-\>) to assign the 17 value to the member variable age. After assigning the value to the member, we then used the arrow operator (\-\>) to access the member and print its value as we can see in the output. ## Examples of Arrow Operators -\> in C++ We will implement several examples of the usage of the \-\> operator in C++ to understand about the arrow operator in more depth. ### Example 1 In this example, we will demonstrate the usage of the arrow operator \-\> in C++ with classes. Here, member functions of the class are accessed by the pointer object using the \-\> operator. **Code** ``` ``` **Output** ``` ``` **Explanation** In the above example, we have first created a Student class that contains some private variables and public functions. The first function which is percentage calculates the total percentage by using the marks stored in the total\_marks variable. Now, as we can see the total\_marks variable is **private** so we can use the this keyword to refer to the private variable. Now for referring to the current object's private variable, the this keyword uses the arrow operator. ### Example 2 In this example, we will demonstrate the usage of the arrow operator \-\> in C++ with **unions**. Here, member variables of the union are accessed by the pointer object using the \-\> operator. **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we created a union called Studentwith some variables. In the main function, the object of the union student is created and initialized with a NULL value. Here, we have dynamically allocated memory to the created union object. Since the object contains two variables we can refer to those variables using the arrow operator. So we have used the object name along with the arrow operator to refer to the desired union variable. ### Example 3 In this example, we will demonstrate the usage of the arrow operator \-\> in C++ with **struct**. Here, member variables of the structure are accessed by the pointer object using the \-\> operator. **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we have created a structure called Employee having a variable name. In the main function, we have the object of the student structure and initialized them with the NULL value. Now, we have dynamically allocated the memory to the union object. Since the object contains one variable we can refer to that variable using the arrow operator. So we have used the object name along with the arrow operator to refer to the desired struct variable. ## Asterisk and Dot Operator Instead of Arrow Operator Let's see an example to understand the usage of the arrow operator and combination of (\*) and (.) operators which is a kind of similar way to access the members of a class. ### Example **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we have created a class called Combihas a variable and member functions. In the main function, we have created the object of the Combi class. Now, we have dynamically allocated the memory to the object s using the new keyword. Since we have used the new keyword the object is created and a reference to the object(pointer) is returned. We can either use the arrow operator or the Asterisk(\*) and dot(.) operator to refer to the data members and member functions. Here, first the \*s is resolved and then the dot operator will help us to locate the actual data member and member functions. Now, we will use the arrow operator to implement the same program. This will also give the same output. **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we have created a class called Combi that has a variable and member functions. In the main function, we have created the object of the Combi class. Now, we have dynamically allocated the memory to the object s using the new keyword. Since we have used the new keyword the object is created and a reference to the object(pointer) is returned. Here, we have used the arrow operator to refer to the desired class variable or member function. ## Different Operators in C++ **Operators** are the basic building block of any programming language. An operator is a symbol that will be operated on a variable or a constant value. They are used to give instructions to the compiler to perform a particular operation. Each operator has its pre-defined operation. There exists a different kinds of operators in C++ which are listed below: - C++ Arithmetic Operators - Arithmetic operators are used to perform arithmetic operations on the operands. These operators include \+ (addition), \- (Subtraction), \* (Multiplication), % (Modulo), and / (Division). - C++ Relational Operators - Relational Operators are used to compare the values of two operands. These operators include \== (Is Equal To), != (Not Equal To), \> (Greater Than), \< (Less Than), \>= (Greater Than or Equal To), \<= (Less Than or Equal To). - C++ Logical Operators - Logical Operators are used to find whether an expression is true or not. These operators include && (Logical AND), \|\| (Logical OR), and \! (Logical NOT). - C++ Bitwise Operators - Bitwise operators are used to performing operations on integers at the bit level. These operators include & (Bitwise AND), \| (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), \<\< (Bitwise Shift Left), \>\> (Bitwise Shift Right). - C++ Assignment Operators - Assignment operators are used for assigning values to a variable. These operators include \=, \+=, \-=, \*=, /=, %=. - and other operators. To learn more about **Operators in C++**, refer to the article [Operators in C++](https://www.scaler.com/topics/cpp/operators-in-cpp/) ## Conclusion - An **operator** is a symbol that is used to give instructions to the compiler to perform a particular operation on operands. - **Arrow operator** (\-\>) in C++ also known as Class Member Access Operator is a combination of two different operators that is Minus operator (-) and greater than operator (\>). - It is used to access the members of a **class**, **structure**, or members of **union** with the help of a pointer variable. - There is a **.(dot)** operator in C++ that is also used to access the members of a class. - . (dot operator) accesses the member or variable directly Whereas instead of accessing the members directly the arrow operator(\-\>) in C++ uses a pointer to access them. - Arrow (\-\>) operator in C++ represents the same meaning which is done by the (\*) asterisk operator and dot(.) operator. - The combination of (\*) and the (.) operator can also be used to access the members of a class similarly. ![topics](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/suggestion_bulb.svg) Got suggestions? [We would love to hear your feedback.]() ![topics](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/filled_bulb_pink.svg)Your feedback is important to help us improve [Close]() [Submit]() [![topics logo](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/logo.svg)](https://www.scaler.com/topics/) A Free learning platform made with ![heart icon](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/icon_heart.svg) by [![scaler logo](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/scaler_logo.svg)](https://www.scaler.com/?utm_source=topics&utm_medium=footer) [![Instagram](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/instagram_footer.svg)](https://www.instagram.com/scaler_official/) [![Youtube](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/youtube_footer.svg)](https://www.youtube.com/c/SCALER) [![Twitter](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/new_twitter.svg)](https://twitter.com/scaler_official) [![Facebook](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/fb_footer.svg)](https://www.facebook.com/scalerofficial) [![Linkedin](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/linkedin_footer.svg)](https://www.linkedin.com/school/scalerofficial/mycompany/) [![Discord](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/discord_footer.svg)](https://discord.com/invite/gD2ZTC5j8K) Explore Scaler - [Academy](https://www.scaler.com/academy/?utm_source=topics&utm_medium=footer) - [Data Science & ML](https://www.scaler.com/data-science-course/?utm_source=topics&utm_medium=footer) - [AI and Machine Learning](https://www.scaler.com/ai-machine-learning-course/?utm_source=topics&utm_medium=footer) - [DevOps and Cloud Computing](https://www.scaler.com/devops-course/?utm_source=topics&utm_medium=footer) - [Neovarsity](https://www.scaler.com/neovarsity/?utm_source=topics&utm_medium=footer) Explore Topics - [Free Online Courses](https://www.scaler.com/topics/courses/) - [Challenges](https://www.scaler.com/topics/challenges/) - [Contest](https://www.scaler.com/topics/contests/) - [Topics](https://www.scaler.com/topics/hubs/) - [Articles](https://www.scaler.com/topics/articles/) - [Events](https://www.scaler.com/topics/events/) Resources - [About Us](https://www.scaler.com/about/?utm_source=topics&utm_medium=footer) - [Blog](https://www.scaler.com/blog/?utm_source=topics&utm_medium=footer) - [Careers](https://www.scaler.com/careers/?utm_source=topics&utm_medium=footer) - [Review](https://www.scaler.com/review/?utm_source=topics&utm_medium=footer) Download the ![scaler logo](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/scaler_logo.svg) app\! Get all scaler resources under one roof\! 4\.4 1\.71 K Reviews 100K+ Downloads ![QR Code](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/qr_codes/topics_footer_web.svg) [![Playstore Icon](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/playstore.svg)](https://app.scaler.com/XBSh) ![Playstore Icon](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/app_download_phone.webp) [![Playstore Icon](https://d1g0iq4cbcvjcd.cloudfront.net/topics/images/playstore.svg)](https://app.scaler.com/Yhwr) Popular Free Certification Courses [Java Course for Beginners](https://www.scaler.com/topics/course/java-beginners/ "Free Java Course Online") [C++ Course with Certificate](https://www.scaler.com/topics/course/cpp-beginners/ "Free C++ Course Online") [Python Course for Beginners](https://www.scaler.com/topics/course/python-for-beginners/ "Free Python Course Online") [Javascript Free Course for Beginners](https://www.scaler.com/topics/course/javascript-beginners/ "Free Javascript Course Online") [Data Science Course for Beginners](https://www.scaler.com/topics/course/python-for-data-science/ "Free Data Science Course Online") [DBMS Course](https://www.scaler.com/topics/course/dbms/ "Free DBMS Course Online") [Python and SQL for Data Science Course](https://www.scaler.com/topics/course/python-sql-data-science/ "Free Python for Data Science Course Online") [DSA Problem Solving for Interviews](https://www.scaler.com/topics/course/dsa-interviews-java/ "Free DSA Java Interview Questions Online") [Instagram System Design Course](https://www.scaler.com/topics/course/instagram-system-design/ "Free Instagram Design Course Online") [Dynamic Programming Course](https://www.scaler.com/topics/course/dynamic-programming/ "Free Dynamic Programming Course Online") [All Free Online Courses](https://www.scaler.com/topics/courses/ "All Free Online Courses") Popular Tutorials [Python Tutorial](https://www.scaler.com/topics/python/ "Python Tutorial Online") [Java Tutorial](https://www.scaler.com/topics/java/ "Java Tutorial Online") [DBMS Tutorial](https://www.scaler.com/topics/dbms/ "DBMS Tutorial Online") [Javascript Tutorial](https://www.scaler.com/topics/javascript/ "Javascript Tutorial Online") [C++ Tutorial](https://www.scaler.com/topics/cpp/ "C++ Tutorial Online") [SQL Tutorial](https://www.scaler.com/topics/sql/ "SQL Tutorial Online") [Software Engineering Tutorial](https://www.scaler.com/topics/software-engineering/ "Software Engineering Tutorial Online") [Data Science Tutorial](https://www.scaler.com/topics/data-science/ "Data Science Tutorial Online") [Pandas Tutorial](https://www.scaler.com/topics/pandas/ "Pandas Tutorial Online") [Deep Learning Tutorial](https://www.scaler.com/topics/deep-learning/ "Deep Learning Tutorial Online") [All Tutorials](https://www.scaler.com/topics/hubs/ "All Tutorials") Compilers [Python Compiler](https://www.scaler.com/topics/python/online-python-compiler/ "Python Compiler Online") [Java Compiler](https://www.scaler.com/topics/java/online-java-compiler/ "Java Compiler Online") [Javascript Compiler](https://www.scaler.com/topics/javascript/online-javascript-compiler/ "Javascript Compiler Online") [C Compiler](https://www.scaler.com/topics/c/online-c-compiler/ "C Compiler Online") [C++ Compiler](https://www.scaler.com/topics/cpp/online-cpp-compiler/ "C++ Compiler Online") Tools [Json Validator](https://www.scaler.com/topics/javascript/json-validator/ "Json Validator Online") [SQL Formatter](https://www.scaler.com/topics/sql/sql-formatter/ "SQL Formatter Online") [XML Formatter](https://www.scaler.com/topics/tools/xml-formatter/ "XML Formatter Online") [CSS Formatter](https://www.scaler.com/topics/css/css-formatter/ "CSS Formatter Online") [JavaScript Formatter](https://www.scaler.com/topics/javascript/javascript-formatter/ "JavaScript Formatter Online") Copyright 2026 InterviewBit Technologies Pvt. Ltd. All Rights Reserved. [Privacy Policy](https://www.scaler.com/privacy/?utm_source=topics&utm_medium=footer) [Terms of Use](https://www.scaler.com/terms/?utm_source=topics&utm_medium=footer) [Contact Us](https://www.scaler.com/contact/?utm_source=topics&utm_medium=footer)
Readable Markdown
Before learning about the **Arrow Operator -\>** in C++, let's learn about the basics of Operators in C++. An **operator** is a symbol that is used to give instructions to the compiler to perform a particular operation on given operands. Each operator has its pre-defined operation. Operators are the basic building blocks of any programming language. There are different kinds of operators in C++, such as Assignment Operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, and many others operators. Now, let's understand what is -\> in C++ (Arrow Operator). **Arrow operator** (\-\>) in C++ also known as Class Member Access Operator is a combination of two different operators that is Minus operator (\-) and greater than operator (\>). It is used to access the public members of a **class**, **structure**, or members of **union** with the help of a pointer variable. There is a .(dot) operator in C++ that is also used to access the members of a class. But .(dot operator) accesses the member or variable directly means without using the pointers whereas instead of accessing members directly the arrow operator(\-\>) in C++ uses a pointer to access them. So the advantage of the \-\> operator is that it can deal with both pointer and non-pointer access but the dot(.) operator can only be used to access the members of a class. Arrow (\-\>) operator in C++ represents the same meaning which is done by the (\*) asterisk operator and dot(.) operator. Arrow operator can be written in a different way which is using the combination of two operators \*.. For example, (\*s).element is the same as s-\>element. The brackets (parenthesis) used while using the combination of (\*.) operators show that the precedence of the dot(.) operator is more than \*. Let's understand the use of the Arrow operator \-\> in C++ using some practical programming examples. ### Syntax Let's first take a look at the syntax for using the arrow operator in C++ for accessing the members of a class, structure, etc. ``` ``` Here, we have used the arrow operator to access the class member variable represented by class\_member\_name through the object pointer which is represented by pointer. ### Explanation Let's implement a simple program to explain and demonstrate the use of the \-\> operator in C++. In the below example, we have declared a **structure** Person which contains two member variables. The first one is the name variable which is a character array that stores the name of the Person and the other one is the age variable which is of integer type. **Code** ``` ``` **Output** ``` ``` **Explanation** In the above example, after creating classes and objects we have called the main() method. In the main() method, we have created an object info of structure type that is Person type. Then we used the arrow operator (\-\>) to assign the 17 value to the member variable age. After assigning the value to the member, we then used the arrow operator (\-\>) to access the member and print its value as we can see in the output. ## Examples of Arrow Operators -\> in C++ We will implement several examples of the usage of the \-\> operator in C++ to understand about the arrow operator in more depth. ### Example 1 In this example, we will demonstrate the usage of the arrow operator \-\> in C++ with classes. Here, member functions of the class are accessed by the pointer object using the \-\> operator. **Code** ``` ``` **Output** ``` ``` **Explanation** In the above example, we have first created a Student class that contains some private variables and public functions. The first function which is percentage calculates the total percentage by using the marks stored in the total\_marks variable. Now, as we can see the total\_marks variable is **private** so we can use the this keyword to refer to the private variable. Now for referring to the current object's private variable, the this keyword uses the arrow operator. ### Example 2 In this example, we will demonstrate the usage of the arrow operator \-\> in C++ with **unions**. Here, member variables of the union are accessed by the pointer object using the \-\> operator. **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we created a union called Studentwith some variables. In the main function, the object of the union student is created and initialized with a NULL value. Here, we have dynamically allocated memory to the created union object. Since the object contains two variables we can refer to those variables using the arrow operator. So we have used the object name along with the arrow operator to refer to the desired union variable. ### Example 3 In this example, we will demonstrate the usage of the arrow operator \-\> in C++ with **struct**. Here, member variables of the structure are accessed by the pointer object using the \-\> operator. **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we have created a structure called Employee having a variable name. In the main function, we have the object of the student structure and initialized them with the NULL value. Now, we have dynamically allocated the memory to the union object. Since the object contains one variable we can refer to that variable using the arrow operator. So we have used the object name along with the arrow operator to refer to the desired struct variable. ## Asterisk and Dot Operator Instead of Arrow Operator Let's see an example to understand the usage of the arrow operator and combination of (\*) and (.) operators which is a kind of similar way to access the members of a class. ### Example **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we have created a class called Combihas a variable and member functions. In the main function, we have created the object of the Combi class. Now, we have dynamically allocated the memory to the object s using the new keyword. Since we have used the new keyword the object is created and a reference to the object(pointer) is returned. We can either use the arrow operator or the Asterisk(\*) and dot(.) operator to refer to the data members and member functions. Here, first the \*s is resolved and then the dot operator will help us to locate the actual data member and member functions. Now, we will use the arrow operator to implement the same program. This will also give the same output. **Code** ``` ``` **Output** ``` ``` **Explanation** In this example, we have created a class called Combi that has a variable and member functions. In the main function, we have created the object of the Combi class. Now, we have dynamically allocated the memory to the object s using the new keyword. Since we have used the new keyword the object is created and a reference to the object(pointer) is returned. Here, we have used the arrow operator to refer to the desired class variable or member function. ## Different Operators in C++ **Operators** are the basic building block of any programming language. An operator is a symbol that will be operated on a variable or a constant value. They are used to give instructions to the compiler to perform a particular operation. Each operator has its pre-defined operation. There exists a different kinds of operators in C++ which are listed below: - C++ Arithmetic Operators - Arithmetic operators are used to perform arithmetic operations on the operands. These operators include \+ (addition), \- (Subtraction), \* (Multiplication), % (Modulo), and / (Division). - C++ Relational Operators - Relational Operators are used to compare the values of two operands. These operators include \== (Is Equal To), != (Not Equal To), \> (Greater Than), \< (Less Than), \>= (Greater Than or Equal To), \<= (Less Than or Equal To). - C++ Logical Operators - Logical Operators are used to find whether an expression is true or not. These operators include && (Logical AND), \|\| (Logical OR), and \! (Logical NOT). - C++ Bitwise Operators - Bitwise operators are used to performing operations on integers at the bit level. These operators include & (Bitwise AND), \| (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), \<\< (Bitwise Shift Left), \>\> (Bitwise Shift Right). - C++ Assignment Operators - Assignment operators are used for assigning values to a variable. These operators include \=, \+=, \-=, \*=, /=, %=. - and other operators. To learn more about **Operators in C++**, refer to the article [Operators in C++](https://www.scaler.com/topics/cpp/operators-in-cpp/) ## Conclusion - An **operator** is a symbol that is used to give instructions to the compiler to perform a particular operation on operands. - **Arrow operator** (\-\>) in C++ also known as Class Member Access Operator is a combination of two different operators that is Minus operator (-) and greater than operator (\>). - It is used to access the members of a **class**, **structure**, or members of **union** with the help of a pointer variable. - There is a **.(dot)** operator in C++ that is also used to access the members of a class. - . (dot operator) accesses the member or variable directly Whereas instead of accessing the members directly the arrow operator(\-\>) in C++ uses a pointer to access them. - Arrow (\-\>) operator in C++ represents the same meaning which is done by the (\*) asterisk operator and dot(.) operator. - The combination of (\*) and the (.) operator can also be used to access the members of a class similarly.
ML Classification
ML Categoriesnull
ML Page Typesnull
ML Intent Typesnull
Content Metadata
Languageen
AuthorMohit Sahay
Publish Time2022-11-21 00:00:00 (3 years ago)
Original Publish Time2022-11-21 00:00:00 (3 years ago)
RepublishedNo
Word Count (Total)1,910
Word Count (Content)1,601
Links
External Links8
Internal Links54
Technical SEO
Meta NofollowNo
Meta NoarchiveNo
JS RenderedNo
Redirect Targetnull
Performance
Download Time (ms)201
TTFB (ms)201
Download Size (bytes)21,614
Shard103 (laksa)
Root Hash2748309851778122103
Unparsed URLcom,scaler!www,/topics/arrow-operator-in-cpp/ s443