Search
 
SCRIPT & CODE EXAMPLE
 

CPP

overload subscript 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 :: visual studio cpp compiler 
Cpp :: sort strings by length and by alphabet 
Cpp :: pass map as reference c++ 
Cpp :: how to grab numbers from string in cpp 
Cpp :: il2cpp stuck unity 
Cpp :: walk filesystem in c++ 
Cpp :: cyclic array rotation in cpp 
Cpp :: initialise 2d vector in c++ 
Cpp :: convert ascii char value to hexadecimal c++ 
Cpp :: google test assert stdout 
Cpp :: c++ remove all elements equal to 
Cpp :: Pseudocode of Dijkstra’s Algorithm in C++ 
Cpp :: What is a ~ in c++ 
Cpp :: set iterator 
Cpp :: iterate const vector 
Cpp :: intersection between vector c++ 
Cpp :: opencv(4.5.1) c:usersappveyorappdatalocal emp1pip-req-build-kh7iq4w7opencvmodulesimgprocsrc esize.cpp:4051: error: (-215:assertion failed) !ssize.empty() in function 
Cpp :: iomanip header file in c++ 
Cpp :: pow without math.h 
Cpp :: C++ Nested if 
Cpp :: ternary operator in c++ 
Cpp :: c++ define function pointer 
Cpp :: phi function (n log (log(n))) 
Cpp :: max circular subarray sum gfg practice 
Cpp :: && c++ 
Cpp :: visual studio code terminal keeps closing c++ 
Cpp :: c ++ The output should be (abc),(def),(ghw) 
Cpp :: Madiar loh 
Cpp :: new expression 
Cpp :: Variabili globali c++ 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =