Given a simple class that overloads the [] operator:
class A { public: int operator[](int p_index) { return a[p_index]; } private: int a[5]; };
I would like to do the following:
void main() { A Aobject; Aobject[0] = 1; // Problem here }
How can I overload the assignment operator '=' in this case to work with the operator [] '?
You do not overload the operator =. You are returning the link.
=
int& operator[](int p_index) { return a[p_index]; }
Be sure to specify the version const:
const
const int& operator[](int p_index) const { return a[p_index]; }
Set link:
int & operator[](int p_index) { return a[p_index]; }
Note that you will also need a const version that returns a value:
int operator[](int p_index) const { return a[p_index]; }
, , vaiable a.
int, .
" C2106: '=': l-value", .
, , .
Please change the return type of the function [] overload to a link or pointer, which will work fine.
Source: https://habr.com/ru/post/1744016/More articles:imap_open dies when called - phpCan I zoom in on this in the following example? - javascriptTextBox watermark in ASP.NET MVC - asp.netCreate fake subdirectories with htaccess and php and save existing directories, like - phpВодяной знак для бритвы MVC3 с использованием Display (Prompt = - asp.net-mvc-3Get current Silverlight client version? - c #Xcode - EXEC_BAD_ACCESS when concatenating a large string - objective-cextract each occurrence in a string - phpSubsonic and .Net 4.0 - c #Validating Predicate Logic Using Coq - Beginner Syntax - predicateAll Articles