参考麻烦双[3]与双* [英] reference trouble in double[3] vs. double*

查看:48
本文介绍了参考麻烦双[3]与双*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


假设我有一个名为Triplet的课程作为信封

for double [3],即。


class Triplet {

public:

Triplet(){/*...*/}

/ *

有些东西加倍[3]没有,比如

a<<运营商将其发送到流中

* /


私人:

双重存储[3];

};


我有下标操作符的问题,我试过了


double& Triplet :: operator [](const unsigned int i)const {

return storage [i];

}


和看到它不能编译,我感到非常惊讶。当我改变

double storage [3]时,它的工作原理很好。 双重存储并分配它

相应。


我想我被数组之间的差异所困扰

的double和指针当涉及到参考资料时要翻倍,

但是有人可以解释为什么我的尝试是不可能的?
不可能?我得到了类似


return(static_cast< double *>(&(storage [0])))[i];


同时也使操作符[]非常量,但它确实看起来很难看。


所以......数组的含义是什么? br />
我想做什么?是否有一种更清洁的方式(将数组更改为指针并且执行新的[]将是矫枉过正的,我使用这些

三胞胎在大型矩阵中达到8100x8100)。

Hi!

Let''s say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn''t have, like
a << operator to send it to a stream
*/

private:
double storage[3];
};

I''m having a problem with the subscript operator, I tried

double& Triplet::operator[](const unsigned int i) const {
return storage[i];
}

and was quite surprised to see it won''t compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.

I guess I''m being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like

return (static_cast<double*>(&(storage[0])))[i];

while also making the operator[] non-const, but it sure looks ugly.

So... what is it with the array that doesn''t allow what
I''m trying to do? Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).

推荐答案



" Jacek Dziedzic" < JA ************* @ janowo.net>在消息中写道

新闻:c5 ********** @ korweta.task.gda.pl ...

"Jacek Dziedzic" <ja*************@janowo.net> wrote in message
news:c5**********@korweta.task.gda.pl...
嗨!

假设我有一个名为Triplet的课程,作为双重[3]的信封
,即。

class Triplet {
public:
Triplet(){/*...*/}
/ *
有些东西加倍[3]没有,比如
a<<操作员将其发送到流中
* /

私人:
双重存储[3];
};

我是我有下标操作符的问题,我试过了

double&三元组::运算符[](const unsigned int i)const {
返回存储[i];
}

并且看到它不能编译时非常惊讶。然而,当我改变
double storage [3]时,它的工作原理很好。 双重存储并相应地分配它。


想一想


const Triplet x;

x [1] = 2.0;


你真的想要编译它吗?

我想我被双子的数组之间的差异和指向双倍的指针所困扰参考,
但有人可以阐明为什么我的尝试不可能吗?我得到了类似

返回(static_cast< double *>(&(storage [0])))[i];

同时也使运营商[]非常规,但它确实看起来很难看。

所以......数组是什么,它不允许我想要做什么?


它与数组无关,返回对任何数据的非const引用
来自const方法的
成员将无法编译。

是否有一种更清洁的方式(将数组更改为指针并执行新的[]将是矫枉过正的,我在8100x8100的巨大矩阵中使用这些三重组。)
Hi!

Let''s say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn''t have, like
a << operator to send it to a stream
*/

private:
double storage[3];
};

I''m having a problem with the subscript operator, I tried

double& Triplet::operator[](const unsigned int i) const {
return storage[i];
}

and was quite surprised to see it won''t compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.
Think about it

const Triplet x;
x[1] = 2.0;

do you really want that to compile?

I guess I''m being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like

return (static_cast<double*>(&(storage[0])))[i];

while also making the operator[] non-const, but it sure looks ugly.

So... what is it with the array that doesn''t allow what
I''m trying to do?
Its nothing to do with arrays, returning a non-const reference to any data
member from a const method will not compile.
Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).




这样做


double& Triplet :: operator [](const unsigned int i){

返回存储[i];

}


double Triplet :: operator [](const unsigned int i)const {

return storage [i];

}


即define运算符的常量和非常量版本[]。


john



Do it like this

double& Triplet::operator[](const unsigned int i) {
return storage[i];
}

double Triplet::operator[](const unsigned int i) const {
return storage[i];
}

i.e. define const and non-const versions of your operator[].

john




" Jacek Dziedzic < JA ************* @ janowo.net>在消息中写道

新闻:c5 ********** @ korweta.task.gda.pl ...

"Jacek Dziedzic" <ja*************@janowo.net> wrote in message
news:c5**********@korweta.task.gda.pl...
嗨!

假设我有一个名为Triplet的课程,作为双重[3]的信封
,即。

class Triplet {
public:
Triplet(){/*...*/}
/ *
有些东西加倍[3]没有,比如
a<<操作员将其发送到流中
* /

私人:
双重存储[3];
};

我是我有下标操作符的问题,我试过了

double&三元组::运算符[](const unsigned int i)const {
返回存储[i];
}

并且看到它不能编译时非常惊讶。然而,当我改变
double storage [3]时,它的工作原理很好。 双重存储并相应地分配它。


想一想


const Triplet x;

x [1] = 2.0;


你真的想要编译它吗?

我想我被双子的数组之间的差异和指向双倍的指针所困扰参考,
但有人可以阐明为什么我的尝试不可能吗?我得到了类似

返回(static_cast< double *>(&(storage [0])))[i];

同时也使运营商[]非常规,但它确实看起来很难看。

所以......数组是什么,它不允许我想要做什么?


它与数组无关,返回对任何数据的非const引用
来自const方法的
成员将无法编译。

是否有一种更清洁的方式(将数组更改为指针并执行新的[]将是矫枉过正的,我在8100x8100的巨大矩阵中使用这些三重组。)
Hi!

Let''s say I have a class called Triplet that serves as an envelope
for double[3], ie.

class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn''t have, like
a << operator to send it to a stream
*/

private:
double storage[3];
};

I''m having a problem with the subscript operator, I tried

double& Triplet::operator[](const unsigned int i) const {
return storage[i];
}

and was quite surprised to see it won''t compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.
Think about it

const Triplet x;
x[1] = 2.0;

do you really want that to compile?

I guess I''m being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like

return (static_cast<double*>(&(storage[0])))[i];

while also making the operator[] non-const, but it sure looks ugly.

So... what is it with the array that doesn''t allow what
I''m trying to do?
Its nothing to do with arrays, returning a non-const reference to any data
member from a const method will not compile.
Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).




这样做


double& Triplet :: operator [](const unsigned int i){

返回存储[i];

}


double Triplet :: operator [](const unsigned int i)const {

return storage [i];

}


即define运算符的常量和非常量版本[]。


john



Do it like this

double& Triplet::operator[](const unsigned int i) {
return storage[i];
}

double Triplet::operator[](const unsigned int i) const {
return storage[i];
}

i.e. define const and non-const versions of your operator[].

john


John Harrison写道:
John Harrison wrote:
这样做

double& Triplet :: operator [](const unsigned int i){
return storage [i];
}
双Triplet :: operator [](const unsigned int i)const {
返回存储[i];
}
即定义运算符[]的常量和非常量版本。
Do it like this

double& Triplet::operator[](const unsigned int i) {
return storage[i];
}

double Triplet::operator[](const unsigned int i) const {
return storage[i];
}

i.e. define const and non-const versions of your operator[].







const

double& Triplet :: operator [](const unsigned int i)const {

return storage [i];

}



Or

const
double& Triplet::operator[](const unsigned int i) const {
return storage[i];
}


这篇关于参考麻烦双[3]与双*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆