问题描述
我在我的一个项目中使用了一个2D矩阵。这类似于。
I'm using a 2D matrix in one of my projects. It's something like it is suggested at C++ FAQ Lite.
整洁的是你可以这样使用:
The neat thing is that you can use it like this:
int main()
{
Matrix m(10,10);
m(5,8) = 106.15;
std::cout << m(5,8);
...
}
现在,并且每个顶点具有公共(仅为了示例的简单)指向如上所述的2D矩阵的指针。现在我有一个非常丑的语法来访问它。
Now, I have a graph composed of vertices and each vertex has a public (just for simplicity of the example) pointer to 2D matrix like above. Now I do have a pretty ugly syntax to access it.
(*sampleVertex.some2DTable)(0,0) = 0; //bad
sampleVertex.some2DTable->operator()(0,0) = 0; //even worse...
可能我缺少一些语法糖由于我缺乏经验操作符重载。是否有更好的解决方案?
Probably I'm missing some syntactic sugar here due to my inexperience with operator overloading. Is there a better solution?
推荐答案
- 考虑使用引用而不是指针
-
考虑为返回2D矩阵引用的顶点创建一个矩阵包装类的getter或实例(提供,它不能为null)。
- Consider using references instead of pointers (provided, it can't be null and you can initialize in the constructor).
Consider making a getter or an instance of a matrix wrapper class for a vertex that returns a reference to 2D matrix (provided, it can't be null).
sampleVertex.some2DTable()(0,0) = 0;
sampleVertex.some2DTableWrap(0,0) = 0;
- 要证明经历所有的麻烦。
However, to me it sounds like a non-issue to justify going through all the trouble.
这篇关于2D矩阵和重载operator()/丑陋语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!