Gra*_*Guy 0 c++ operator-overloading
我在为对象中的读写重写operator []时遇到了麻烦.这是一个包含不同组件的大型代码,我不打算将所有细节放在这里,因为它不会有用.简而言之,我所拥有的是以下内容
class MyObject(){
inline SetterProxy& operator[](int i) {
SetterProxy a(i);
return a;
}
inline double operator[](int i) const{
return some_value;
}
}
Run Code Online (Sandbox Code Playgroud)
第一个重载[]适用于赋值(如果你想知道什么是SetterProxy,我必须使用Proxy类来分配值之前能够进行一些检查和内部函数调用).但是,应该在读取时调用的第二个不起作用并且代码崩溃.我不确定这里发生了什么,但是当我评论第一个时,它运行正常!可能是编译器以某种方式混淆了两者,因为它们都是内联的?
任何想法将不胜感激.
编辑:好的,这里是SetterProxy本身:
class SetterProxy{
private:
Vec v;
int i;
double *ptr_val;
public:
inline SetterProxy(Vec v_, int i_) {
v = v_;
i = i_;
VecGetArray(v,&ptr_val);
}
inline ~SetterProxy(){
VecRestoreArray(v,&ptr_val);
}
inline void operator=(double rhs ){
ptr_val[i] = rhs;
}
};
Run Code Online (Sandbox Code Playgroud)
虽然我不认为它直接来自那个.最初我还有它按价值返回,虽然我把它改成参考会更有效率.我认为这应该是安全的,因为赋值是在Proxy operator =()类中完成的,之后代理类超出了范围.无论哪种方式,这都不能解决我的问题!
| 归档时间: |
|
| 查看次数: |
518 次 |
| 最近记录: |