Search
 
SCRIPT & CODE EXAMPLE
 

CPP

overload array operator cpp

// It is idiomatic to provide couple of overloads of the operator[] function
// 		- one for const objects and one for non-const objects.
// The return type of the const member function can be a const& or
// just a value depending on the object being returned while the return type
// of the non-const member function is usually a reference.

struct Heap{
    int H[100];
    int operator [] (int i) const {return H[i];}
    int& operator [] (int i) {return H[i];}
};

//This allows you to modify a non-const object using the array operator...

Heap h1;
h1[0] = 10;

//...while still allowing you to access const objects.

Heap const h2 = h1;
int val = h2[0];

// https://stackoverflow.com/questions/37043078/c-overloading-array-operator
Comment

PREVIOUS NEXT
Code Example
Cpp :: do while c++ 
Cpp :: exponent power of x using c c++ 
Cpp :: preorder 
Cpp :: how to grab each character from a string 
Cpp :: how to sort array in c++ 
Cpp :: install qpid broker in ubuntu/linux 
Cpp :: vector from angle 
Cpp :: how to copy vector to another vector in c++ 
Cpp :: set of vectors c++ 
Cpp :: C++ program for Celsius to Fahrenheit and Fahrenheit to Celsius conversion using class 
Cpp :: 1768. Merge Strings Alternately leetcode solution in c++ 
Cpp :: how to check if vector is ordered c++ 
Cpp :: max c++ 
Cpp :: string in c++ 
Cpp :: three way comparison operator c++ 
Cpp :: stoi in c++ 
Cpp :: how to create an integer in c++ 
Cpp :: count number of char in a string c++ 
Cpp :: shortest path in unweighted graph bfs 
Cpp :: cpp vector popback 
Cpp :: first and last digit of a number in c++ 
Cpp :: how to insert in a queue c++ 
Cpp :: ex: cpp 
Cpp :: << in C++ 
Cpp :: error in c++ 
Cpp :: how togreper 
Cpp :: c++ void poiinter 
Cpp :: error: use of parameter outside function body before ] token c++ 
Cpp :: Bit Tricks for Competitive Programming c ++ 
Cpp :: gdb get return value of function 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =