-1

How do I meet below objective in C++ language. My objective is to have an input source (a structure) defined in a constructor (or another method of same class) and access it in another method for processing. For Eg:

#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
}

Any alternate approach meeting above objective is most welcome. Thanks in advance for the help.

Jaydeep
  • 11
  • 1

1 Answers1

1

It seems the best approach is to make the static constant variable a public member of the class, and then access it normally. See this post for more info:

C++ where to initialize static const

Community
  • 1
  • 1
polfosol ఠ_ఠ
  • 1,840
  • 26
  • 41