How to access a public member variable from another class ?

J

Jurek Dabrowski

hi all,

I am trying to access a public variable "m_bHaveAnswer" declared in class
foo from class foo2. Class foo2 declares a pointer to the class foo. This
compiles fine, but when I try to step through the code in debug mode,
"m_bHaveAnswer" cannot be evaluated from within class foo2 via the pointer
to class foo. Any ideas ?

thanks,

jurek

file foo1.h

class foo

{

public:

foo();

~foo();

public:

bool m_bHaveAnswer;

};

file foo2.h

class foo2

{

public:

foo2();

~foo2();

public:

foo* fooPtr;

};

file foo1.cpp

#include "foo11.h"

using namespace std;

foo::foo()

{

cout << "foo constuctor called\n";

}

foo::~foo()

{

cout << "foo destuctor called\n";

}

file foo2.cpp

#include "foo2.h"

using namespace std;

foo2::foo2()

{

cout << "foo constuctor called\n";

}

foo2::~foo2()

{

cout << "foo destuctor called\n";

}

file Class_Test.cpp

using namespace std;

#include "foo1.h"

#include "foo2.h"

int _tmain(int argc, _TCHAR* argv[])

{

foo2 f2;


f2.fooPtr->m_bHaveAnswer = 0;

cout << "f2.fooPtr->m_bHaveAnswer = " << f2.fooPtr->m_bHaveAnswer << endl;


return 0;

}
 
J

John Harrison

Jurek said:
hi all,

I am trying to access a public variable "m_bHaveAnswer" declared in class
foo from class foo2. Class foo2 declares a pointer to the class foo. This
compiles fine, but when I try to step through the code in debug mode,
"m_bHaveAnswer" cannot be evaluated from within class foo2 via the pointer
to class foo. Any ideas ?

thanks,

jurek

file foo1.h

class foo

{

public:

foo();

~foo();

public:

bool m_bHaveAnswer;

};

file foo2.h

class foo2

{

public:

foo2();

~foo2();

public:

foo* fooPtr;

};

file foo1.cpp

#include "foo11.h"

using namespace std;

foo::foo()

{

cout << "foo constuctor called\n";

}

foo::~foo()

{

cout << "foo destuctor called\n";

}

file foo2.cpp

#include "foo2.h"

using namespace std;

foo2::foo2()

{

cout << "foo constuctor called\n";

}

foo2::~foo2()

{

cout << "foo destuctor called\n";

}

file Class_Test.cpp

using namespace std;

#include "foo1.h"

#include "foo2.h"

int _tmain(int argc, _TCHAR* argv[])

{

foo2 f2;


f2.fooPtr->m_bHaveAnswer = 0;

cout << "f2.fooPtr->m_bHaveAnswer = " << f2.fooPtr->m_bHaveAnswer << endl;


return 0;

}

Although everything compiles fine, at no point do you give f2.fooPtr a
value. To put it bluntly, there is no foo object, so how did you expect
to get a valid pointer to one?

This would work

foo f;
foo2 f2;
f2->fooPtr = &f;
f2.fooPtr->m_bHaveAnswer = 0;

john
 
J

John Harrison

This would work

foo f;
foo2 f2;
f2->fooPtr = &f;
f2.fooPtr->m_bHaveAnswer = 0;

Sorry, typo

foo f;
foo2 f2;
f2.fooPtr = &f;
f2.fooPtr->m_bHaveAnswer = 0;

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top