Har*_*ngh 3 c++ operator-overloading
我正在编写一些时髦的音频代码,并试图使用运算符重载来创建一个非常简洁的API.它已成为一个C++脑力激荡器......
我想要的将立即通过"分配到索引"的复合运算符来解决,我很确定它不存在.任何人都可能对以下是否可能有任何见解?
我有2种对象类型....
Frames frames; // audio data, contains 1 buffer (float *) for each channel
Sample s; // a single sample, 1 float for each channel
Run Code Online (Sandbox Code Playgroud)
所以Sample是帧的正交切片,即帧不是一个数组Sample.如果您知道音频,Frames则是"非交错",并且Sample是.
圣杯...
s = frames[1]; // statement 1. gets a copy of values in frame 1
frames[1] = s; // statement 2. replace values in frame 1 with those in Sample s
Run Code Online (Sandbox Code Playgroud)
第一个没问题:
// in class Frames...
Sample& operator[](size_t idx) const {
Sample s;
s.left = _leftCh[idx];
s.right = _rightCh[idx];
return s;
}
Run Code Online (Sandbox Code Playgroud)
但是第二个赋值是棘手的,因为上面的函数创建了数据的副本而不是引用.
我试过用引用定义Sample ...
class Sample {
public:
float& L;
float& R;
Sample(float& lt, float& rt) : L(lt), R(rt) {};
}
Run Code Online (Sandbox Code Playgroud)
但是你不能做一些简单的事情......
Sample s(0.0, 0.0);
s.left = 0.2;
Run Code Online (Sandbox Code Playgroud)
另一个可能的解决方案是让两个语句调用两个不同的运算符重载.然后强制执行该语句2调用this []重载,该重载返回一个指向值而不是Sample对象的新Frames 对象:
Frames& operator[](size_t idx) {
// Construct an new Frames object whose which
// points to value idx in each channel
Frames f(_size - idx);
f._leftCh = &_leftCh[idx];
f._rightCh = &_rightCh[idx];
return f;
}
Run Code Online (Sandbox Code Playgroud)
然后添加一个赋值运算符Frames,只替换第一个值...
Frames& operator=(const Sample& s) {
_leftCh[0] = s.left;
_rightCh[0] = s.right;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
编译器通知我方法必须不仅仅是返回类型,但这可以通过const在其中一个operator[]重载的方法名称后面来解决.可能在这里有线索吗?有没有办法让声明1呼叫Sample& operator[]...和声明2呼叫Frames& operator[]....还是有更好的方法来实现这个?
如果你已经做到这一点,感谢你的耐心等待!非常感激...
这个怎么样:
class SampleRef {
float &left_, &right_;
public:
SampleRef(float& left, float& right)
: left_(left), right_(right)
{}
operator Sample () {
return Sample(left_, right_);
}
SampleRef& operator= (const Sample &arg) {
left_ = arg.left;
right_ = arg.right;
return *this
}
};
SampleRef Frames::operator[] (size_t idx) {
return SampleRef(_leftCh[idx], _rightCh[idx]);
}
Run Code Online (Sandbox Code Playgroud)
您当然也可以添加一个const重载,operator[]它只会返回一个Sample:
Sample Frames::operator[] (size_t idx) const {
return Sample(_leftCh[idx], _rightCh[idx]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |