473,388 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to access a public member variable from another class ?

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
3 7118
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
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: jt | last post by:
Being a newbie in C++ and comming from C, I can't find information on how to access a class member function outside its class. Below is a snippet of the class and its member function: look at...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
3
by: Michael Ramey | last post by:
How can controls on a Windows Form be accessed (or referenced) from another Class? I know how to do it from another Form. The following doesn't work even though the Control Modifiers property is...
13
by: Liz | last post by:
ok, this is really simple stuff, or it should be ... but I'm stuck In a Windows Forms app, I have something resembling this: Form1.cs ======== namespace NS Class Form1
3
by: Samuel Burri | last post by:
Hi there I got a simple problem. As you can see in the posted source, I have two classes, where one is derived from the other. In fact, they also contain virtual functions. In the function...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
9
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.