如何在C++语言中实现以下目标。我的目标是在构造函数(或同一类的另一个方法)中定义一个输入源(一个结构),并在另一种方法中访问它进行处理。例如:
#include "iostream"
class A
{
public:
struct source{ //input source
char* input;
unsigned int result;
};
A(); //constructor
~A(); //destructor
void process();
};
A::A()
{
//static local input source
static const source inp[2] = { {"input1", 2}, {"input2", 3} };
}
void A::process()
{
//The value of static structure "inp" initialized in constructor is to be
// read here.
// Say I want to print the "result"
std::cout << "input1 result" << inp[0].result; //should print 2
std::cout << "input2 result" << inp[1].result; //should print 3
}
我们非常欢迎以上目标的任何替代方法会议。提前感谢您的帮助。
似乎最好的方法是使静态常量变量成为类的公共成员,然后正常访问它。查看这篇文章了解更多信息:
C++在哪里初始化静态常量