c++ function scope
- by Myx
I have a main function in A.cpp which has the following relevant two lines of code:
B definition(input_file);
definition.Print();
In B.h I have the following relevant lines of code:
class B
{
public:
// Constructors
B(void);
B(const char *filename);
~B(void);
// File input
int ParseLSFile(const char *filename);
// Debugging
void Print(void);
// Data
int var1;
double var2;
vector<char* > var3;
map<char*, vector<char* > > var4;
}
In B.cpp, I have the following function signatures (sorry for being redundant):
B::B(void)
: var1(-1),
var2(numeric_limits<double>::infinity())
{
}
B::B(const char *filename)
{
B *def = new B();
def->ParseLSFile(filename);
}
B::~B(void)
{
// Free memory for var3 and var 4
}
int B::ParseLSFile(const char *filename)
{
// assign var1, var2, var3, and var4 values
}
void B::Print(void)
{
// print contents of var1, var2, var3, and var4 to stdout
}
So when I call Print() from within B::ParseLSFile(...), then the contents of my structures print correctly to stdout. However, when I call definition.Print() from A.cpp, my structures are empty or contain garbage. Can anyone recommend the correct way to initialize/pass my structures so that I can access them outside of the scope of my function definition?
Thanks.