user970500
user970500

Reputation: 581

How to access variale defined in different class in c++

I am using xerces library to parse xml in C++

storing xml elements into string array, I want to access this string array from my class

code of Handler class -

#include "MySAX2Handler.hpp"
#include <xercesc/sax2/Attributes.hpp>
#include <iostream>
#include <string>

using namespace std;
const int MAXITEMS = 100;
string resultArray[MAXITEMS];
int cnt = 0;

void MySAX2Handler::startElement(const XMLCh* const uri, const XMLCh* const localname,
const XMLCh* const qname, const Attributes& attrs)
{
  char* message = XMLString::transcode(localname);
  resultArray[cnt] = message;
  cnt++;
  for (int idx = 0; idx < attrs.getLength(); idx++)
  {
    char* attrName = XMLString::transcode(attrs.getLocalName(idx));
    char* attrValue = XMLString::transcode(attrs.getValue(idx));
    resultArray[cnt] = attrName;
    cnt++;
    resultArray[cnt] = attrValue;
    cnt++;
  }
  XMLString::release(&message);
}

I want to access resultArray from another class

Please help me I am new to C++

Upvotes: 1

Views: 123

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477512

resultArray is a global variable with external linkage, so you can already access it from anywhere in your program. You just have to declare it:

// someotherfile.cpp
extern std::string resultArray[100];

void foo()
{
  std::cout << resultArray[12] << std::endl;
}

Upvotes: 2

Related Questions