I want to overload assigning a value to an overloaded index in a c++ class. Here is my code so far:
<code>#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Test {
public:
void operator=(vector<string> val) {
a = val[0];
b = val[1];
c = val[2];
}
string operator[](int index) {
if (index == 0) {
return a;
}
else if (index == 1) {
return b;
}
else {
return c;
}
}
private:
string a = "";
string b = "";
string c = "";
};
int main()
{
// What I can do
Test values;
values = { "X","Y","Z" };
cout << values[0] << endl;
cout << values[2] << endl;
//What I want to do:
// values[1] = "XYZ";
}
</code>
<code>#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Test {
public:
void operator=(vector<string> val) {
a = val[0];
b = val[1];
c = val[2];
}
string operator[](int index) {
if (index == 0) {
return a;
}
else if (index == 1) {
return b;
}
else {
return c;
}
}
private:
string a = "";
string b = "";
string c = "";
};
int main()
{
// What I can do
Test values;
values = { "X","Y","Z" };
cout << values[0] << endl;
cout << values[2] << endl;
//What I want to do:
// values[1] = "XYZ";
}
</code>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Test {
public:
void operator=(vector<string> val) {
a = val[0];
b = val[1];
c = val[2];
}
string operator[](int index) {
if (index == 0) {
return a;
}
else if (index == 1) {
return b;
}
else {
return c;
}
}
private:
string a = "";
string b = "";
string c = "";
};
int main()
{
// What I can do
Test values;
values = { "X","Y","Z" };
cout << values[0] << endl;
cout << values[2] << endl;
//What I want to do:
// values[1] = "XYZ";
}
I want to be able to assign, not just get the value of the index.
How could i do this? Ex.
<code>values[1] = "XYZ"
</code>
<code>values[1] = "XYZ"
</code>
values[1] = "XYZ"