assign a value to object with vector operator[]

Hi,

I define a vector<int> ivec.
After;

ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(3);
ivec.push_back(4);

now I'm trying to access.

ivec[2] = 20;

I want to ask.How does this work.
I with [] how do I value.
ivec[2] = 20 When I say;

How calling a function.
Do you have friends wrote this function?
Very good idea to share valuable information on this subject.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
struct floatt{
	float f[4];
	floatt(float f1, float f2, float f3, float f4)
	{
		f[0] = f1;
		f[1] = f2;
		f[2] = f3;
		f[3] = f4;
	}

	floatt(const floatt &r)
	{
		f[0] = r.f[0];
		f[1] = r.f[1];
		f[2] = r.f[2];
		f[3] = r.f[3];
	}

	floatt &operator=(const floatt &r)
	{
		f[0] = r.f[0];
		f[1] = r.f[1];
		f[2] = r.f[2];
		f[3] = r.f[3];
		return *this;
	}

};



class Vec4{
	vector<floatt> fval;
public:
	Vec4()
	{
		fval.push_back(floatt(rand() % 2 + 2.1f, rand() % 3 + 1.7f,rand() % 5 + 7.3f, rand() % 3 + 4.9f));
	}

	Vec4(floatt f)
	{
		fval.push_back(floatt(f));
	}
	Vec4(const Vec4 &f)
	{
		fval = f.fval;
	}

	Vec4 &operator=(const Vec4 &f)
	{
		fval =  f.fval;
		return *this;
	}


	void display()const
	{
		cout << "("<< fval[0].f[0] << ")" << "(" << fval[0].f[1] << ")" <<  "(" <<fval[0].f[2] << ")"<<"("<<fval[0].f[3]<< ")" << endl;

	}

	Vec4 &operator+(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] += v.fval[0].f[0];
		result.fval[0].f[1] += v.fval[0].f[1];
		result.fval[0].f[2] += v.fval[0].f[2];
		result.fval[0].f[3] += v.fval[0].f[3];
		
		return result;
	}

	Vec4 &operator-(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] -= v.fval[0].f[0];
		result.fval[0].f[1] -= v.fval[0].f[1];
		result.fval[0].f[2] -= v.fval[0].f[2];
		result.fval[0].f[3] -= v.fval[0].f[3];
		
		return result;
	}

	Vec4 &operator/(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] /= v.fval[0].f[0];
		result.fval[0].f[1] /= v.fval[0].f[2];
		result.fval[0].f[2] /= v.fval[0].f[3];
		result.fval[0].f[3] /= v.fval[0].f[4];
		
		return result;
	}

	Vec4 &operator*(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] *= v.fval[0].f[0];
		result.fval[0].f[1] *= v.fval[0].f[1];
		result.fval[0].f[2] *= v.fval[0].f[2];
		result.fval[0].f[3] *= v.fval[0].f[3];
		
		return result;
	}

	
	Vec4 &operator+=(const Vec4 &v)
	{
		this->fval[0].f[0] += v.fval[0].f[0];
		this->fval[0].f[1] += v.fval[0].f[1];
		this->fval[0].f[2] += v.fval[0].f[2];
		this->fval[0].f[3] += v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator-=(const Vec4 &v)
	{
		this->fval[0].f[0] -= v.fval[0].f[0];
		this->fval[0].f[1] -= v.fval[0].f[1];
		this->fval[0].f[2] -= v.fval[0].f[2];
		this->fval[0].f[3] -= v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator*=(const Vec4 &v)
	{
		this->fval[0].f[0] *= v.fval[0].f[0];
		this->fval[0].f[1] *= v.fval[0].f[1];
		this->fval[0].f[2] *= v.fval[0].f[2];
		this->fval[0].f[3] *= v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator/=(const Vec4 &v)
	{
		this->fval[0].f[0] /= v.fval[0].f[0];
		this->fval[0].f[1] /= v.fval[0].f[1];
		this->fval[0].f[2] /= v.fval[0].f[2];
		this->fval[0].f[3] /= v.fval[0].f[3];

		return *this;

	}


	float operator [] (int index)	
	{ 	

		return  this->fval[0].f[index];
	
	}

	friend ostream &operator<<(ostream &os, Vec4 &v);
	friend istream &operator>>(istream &is, Vec4 &v);



};


ostream &operator<<(ostream &os, Vec4 &v)
{
	return  os << "(" << v.fval[0].f[0] << ")"  << "(" << v.fval[0].f[1] << "(" << v.fval[0].f[2] << ")" << "(" << v.fval[0].f[3] << ")" <<   endl;

}

istream &operator>>(istream &is, Vec4 &v)
{
	return is >>  v.fval[0].f[0] >> v.fval[0].f[1] >> v.fval[0].f[2] >> v.fval[0].f[3];
}

int main()
{

Vec4 v1(floatt(1.1f, 6.9f, 4.4f, 3.5f));
v1[3] = 3.45;

return 0;
}


v1[3] = returns an error.

How do I resolve this.

float operator [] (int index)
{

return this->fval[0].f[index];

}

How can I write this function again.




Last edited on
You need to return a reference - that way you can use the the [] on the right hand side or left hand side of an assignment expression.

1
2
3
4
5
6
7
    
float& operator [] (int index) //return a reference
{ 

    return  this->fval[0].f[index];

}
thanks

Why should I get the reference.
Topic archived. No new replies allowed.