By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,415 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,415 IT Pros & Developers. It's quick & easy.

How to access a public member variable from another class ?

P: n/a
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;

}
Feb 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jurek Dabrowski wrote:
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
Feb 18 '07 #2

P: n/a
>
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
Feb 18 '07 #3

P: n/a
John,

that fixed it. thanks for your help.

jurek

"John Harrison" <jo*************@hotmail.comwrote in message
news:9n******************@newsfe7-win.ntli.net...
>>
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

Feb 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.