c++ operator[] 重载问题(工作正常,但不适用于指针,为什么?)
C ++中的operator []问题,我有一些类:
197 class Permutation{
198 private:
199 unsigned int* array;
200 unsigned int size;
201
202 void fill(){
203 for(unsigned int i=0;i<size;i++)
204 array[i]=i;
205 }
206 void init(const unsigned int s){
207 if(s){
208 array=new unsigned int[s];
209 size=s;
210 }else{
211 size=0;
212 array=0;
213 }
214 }
215 void clear(){
216 if(array){
217 delete[]array;
218 array=0;
219 }
220 size=0;
221 }
222 public:
223 Permutation(const unsigned int& s=0):array(0),size(0){
224 init(s);
225 fill();
226 }
227 ~Permutation(){
228 clear();
229 }
230 unsigned int& operator[](const unsigned int& idx){
231 assert(idx<size);
232 return array[idx];
233 }
234 unsigned int& get(const unsigned int& idx)
235 {
236 assert(idx<size);
237 return array[idx];
238 }
253 Permutation& operator=(const Permutation& p){
254 clear();
255 init(p.size);
256 size=p.size;
257 for(unsigned int i=0;i<size;i++)
258 array[i]=p.array[i];
259 return *this;
260 }
261
262 Permutation(const Permutation&p)
263 {
264 clear();
265 init(p.size);
266 size=p.size;
267 for(unsigned int i=0;i<size;i++)
268 array[i]=p.array[i];
269 }
};
当我使用
Permutation x(3);
x[0]=1;
它时效果很好,但是当我使用:
Permutation* x=new Permutation(3);
x->get(0)=10; // this works fine
x[0]=1;
在这种情况下,在调试器中我看到它被称为排列类的新对象的构造函数,发生了什么?为什么? 我有人知道发生了什么事,我将不胜感激。
Problem with operator[] in c++, i have some class:
197 class Permutation{
198 private:
199 unsigned int* array;
200 unsigned int size;
201
202 void fill(){
203 for(unsigned int i=0;i<size;i++)
204 array[i]=i;
205 }
206 void init(const unsigned int s){
207 if(s){
208 array=new unsigned int[s];
209 size=s;
210 }else{
211 size=0;
212 array=0;
213 }
214 }
215 void clear(){
216 if(array){
217 delete[]array;
218 array=0;
219 }
220 size=0;
221 }
222 public:
223 Permutation(const unsigned int& s=0):array(0),size(0){
224 init(s);
225 fill();
226 }
227 ~Permutation(){
228 clear();
229 }
230 unsigned int& operator[](const unsigned int& idx){
231 assert(idx<size);
232 return array[idx];
233 }
234 unsigned int& get(const unsigned int& idx)
235 {
236 assert(idx<size);
237 return array[idx];
238 }
253 Permutation& operator=(const Permutation& p){
254 clear();
255 init(p.size);
256 size=p.size;
257 for(unsigned int i=0;i<size;i++)
258 array[i]=p.array[i];
259 return *this;
260 }
261
262 Permutation(const Permutation&p)
263 {
264 clear();
265 init(p.size);
266 size=p.size;
267 for(unsigned int i=0;i<size;i++)
268 array[i]=p.array[i];
269 }
};
when I use
Permutation x(3);
x[0]=1;
it works very well, but when I use:
Permutation* x=new Permutation(3);
x->get(0)=10; // this works fine
x[0]=1;
in this case, in debugger I see it is called a constructor of new object for Permutation class, what is going on ? and why?
I someone know what is going about I would appreciate for information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,你的代码:
然后你这样做:
你正在做的是将指针 x 视为一个数组,并初始化它,这是普通写法:
你想要写的是:
或者,等效的:
First, your code:
And then you do this:
And what you are doing is treating the pointer x as an array, and initializing it, which is longhand for:
What you meant to write was:
Or, equivalent:
对于指针来说,
x[0]
等价于*(x+0)
,后者又等价于*x
。因此,您实际上是在分配给Permutation
对象。由于您要分配值 1,因此将使用
Permutation(const unsigned int&)
转换构造函数。这将创建一个Permutation
类型的临时对象,然后使用赋值运算符将其复制到对象*x
中。For pointers,
x[0]
is equivalent to*(x+0)
, which is equivalent to*x
. So you're actually assigning to thePermutation
object.Since you're assigning a value 1, the
Permutation(const unsigned int&)
conversion constructor is used. This creates a temporary of typePermutation
, which is then copies into the object*x
, using your assignment operator.我想提供另一种选择,让您可以更轻松地编写此内容。您可以创建一个引用,而不是使用指针:
如果您仅在一个地方使用重载运算符,那么这就太过分了。但我发现当你在很多地方使用重载运算符时这个技巧非常方便。
I wanted to present one other option on how you can write this a bit easier. Instead of working with the pointer, you can create a reference:
If you are using an overloaded operator in just one place, this is overkill. But I find this trick quite convenient when you are using overloaded operators in many places.