如何访问 C++ 范围的变量输出?
How do I access variables output of a scope in 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 的值,并且我想避免使用全局变量。有什么解决办法吗?
谢谢
由于您已将所有 class 成员声明为 public,您可以直接通过 Obj.positionX
等访问它们。这不是很好的做法,但给出您的示例代码最简单的方法。
您也可以使用 struct
返回多个值:
#include <iostream>
#include <vector>
using namespace std;
// using a struct for returning multiple variables
struct Decl
{
vector<int> posX;
vector<int> posY;
};
class GetDepth_Class
{
// these are private members now
vector<int> positionX, positionY;
vector<int>::iterator it;
public:
Decl getDepth();
};
Decl GetDepth_Class::getDepth()
{
Decl d;
it = positionX.begin();
positionX.insert(it, 80);
it = positionY.begin();
positionY.insert(it, 74);
d = {positionX, positionY};
for (int i = 0; i < positionX.size(); i++)
cout << positionX[i] << "," << positionY[i] << endl;
return d;
}
int main()
{
GetDepth_Class Obj;
Decl x = Obj.getDepth();
for (int i = 0; i < x.posX.size(); i++)
cout << x.posX[i] << ' ';
cout << endl;
for (int i = 0; i < x.posY.size(); i++)
cout << x.posY[i] << endl;
return 0;
}
我知道这有点乱但是可以理解。
我设置了 {positionX, positionY}
并返回它并使用循环在 main()
中读取它。
Sample Output:
80,74 // printed from class function
80 // positionX from main()
74 // positionY from main()
例如,
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 的值,并且我想避免使用全局变量。有什么解决办法吗?
谢谢
由于您已将所有 class 成员声明为 public,您可以直接通过 Obj.positionX
等访问它们。这不是很好的做法,但给出您的示例代码最简单的方法。
您也可以使用 struct
返回多个值:
#include <iostream>
#include <vector>
using namespace std;
// using a struct for returning multiple variables
struct Decl
{
vector<int> posX;
vector<int> posY;
};
class GetDepth_Class
{
// these are private members now
vector<int> positionX, positionY;
vector<int>::iterator it;
public:
Decl getDepth();
};
Decl GetDepth_Class::getDepth()
{
Decl d;
it = positionX.begin();
positionX.insert(it, 80);
it = positionY.begin();
positionY.insert(it, 74);
d = {positionX, positionY};
for (int i = 0; i < positionX.size(); i++)
cout << positionX[i] << "," << positionY[i] << endl;
return d;
}
int main()
{
GetDepth_Class Obj;
Decl x = Obj.getDepth();
for (int i = 0; i < x.posX.size(); i++)
cout << x.posX[i] << ' ';
cout << endl;
for (int i = 0; i < x.posY.size(); i++)
cout << x.posY[i] << endl;
return 0;
}
我知道这有点乱但是可以理解。
我设置了 {positionX, positionY}
并返回它并使用循环在 main()
中读取它。
Sample Output:
80,74 // printed from class function
80 // positionX from main()
74 // positionY from main()