c++ - 如何在 C++ 中访问范围的变量输出?
问题描述
例如,
class GetDepth_Class {
public:
vector<int> positionX, positionY;
vector<int>::iterator it;
int getDepth();
};
int GetDepth_Class::getDepth(){
......
......
if (scalar[0] == 0 && scalar[1] == 0 && scalar[2] == 0) {
it = positionX.begin();
positionX.insert(it, i + 80);
it = positionY.begin();
positionY.insert(it, j + 80);
}
for (int i = 0; i < positionX.size(); i++) {
cout << positionX[i] << "," << positionY[i] << endl;
}
return EXIT_SUCCESS;//realsense depth camera module
}
int main() {
GetDepth_Class Obj;
Obj.getDepth();
//Here I would like to access the values of vector positionX and vector positionY output from GetDepth_Class::getDepth(),
//how should I do if I want to avoid using global variable?
}
我想从 main() 中的 getDepth() 访问向量 positionX 和向量 positionY 输出的值,并且我想避免使用全局变量。有什么解决办法吗?
谢谢
解决方案
由于您已将所有类成员声明为公共,因此您可以直接通过Obj.positionX
etc 访问它们。这不是很好的做法,但给出的示例代码是最简单的方法。
推荐阅读
- mysql - mysql order by field order 不符合我的期望
- javascript - React-redux 存储状态和 mapStateToProps 状态不同的原因是什么?
- r - 如何在ggplot图例中显示与箱线图分开的异常值和平均线?
- python - 如何在 Python 中任意扩展 Symfit 参数的数量
- autodesk-viewer - 将 navisworks 剖面转换为锻造
- ada - 如何在 Ada 中动态创建固定大小的数组?
- security - 如何使用 BouncyCastle 获取/创建 TlsCredentialedSigner?
- python - 包含括号中偶尔逗号的列的 CSV 文件使 pandas.read_csv 崩溃
- python - 如何从海龟图像中删除箭头状光标
- javascript - 如何在 mobx-router 中传递多个可选参数