arrow operator c++
C++
// C program to show Arrow operator
// used in structure
#include <stdio.h>
#include <stdlib.h>
// Creating the structure
struct student {
char name[80];
int age;
float percentage;
};
// Creating the structure object
struct student* emp = NULL;
// Driver code
int main()
{
// Assigning memory to struct variable emp
emp = (struct student*)
malloc(sizeof(struct student));
// Assigning value to age variable
// of emp using arrow operator
emp->age = 18;
// Printing the assigned value to the variable
printf("%d", emp->age);
return 0;
}
(pointer_name)->(variable_name)/*
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(¬_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
}
Also in C++:
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- delete memory c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- elseif c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- How to traverse in a tree iterative C++
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- ue4 c++ array
- Category
- C++
- Title
- c++ vector
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- what is atoi in strinf
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- root to leaf path print
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- C++ while
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- lower bound c++ for array in decreasing order
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- find_if c++
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- nth_element c++
- Category
- C++
- Title
- c++ reset stream
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- matrix transpose tiling
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- equal elements in two arrays in c++
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- sfml basic program
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- loop through words in string c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- how to inject a dll into a game c++
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- char to string c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- -> cpp
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- find number of 1s in a binary cv::mat image
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- linkedlist implementation in c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- sleep system function linux c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++