[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Getting a pointer to a value?

Hi guys,
  I am stuck again....  I am trying to get a pointer (in CDialog1) to a integer value (in CClass2) so
I should declare --> int *intval; < in CDialog1> and then point  intval = Class2.intval; where intval
is a object of int, not a pointer. BUT... because the prog I am maintaining has many many classes,
and the header files are all mixed/messed up, I cannot cleanly compile my prog if I include "Class.h"
in my Dialog1.h but it is okay if I include "Class.h" in my Dialog1.cpp file ...
which also means that I can create a Class2 object in a local function(.cpp file), but not globally ( .h file)

my question is other that cleaning up my header files mess ( which I don;t have time :) ),
How can I get a pointer to a variables value of the CClass2 ( note: there will always be only one
instance of the object in the application lifetime) without explicitly declaring a object of the CClass2
in my CDialog2????
Please give me some ideas, or even new ways to deal with it...

Thanks a lot!!!!

David C


0
Haho
Asked:
Haho
  • 2
  • 2
1 Solution
 
amvCommented:
Uff... your message is very entangled on my point :)
but i think you should replace  
intval = Class2.intval; with
*intval = Class2.intval; that's all. :)
have a fun.
Mikle.
0
 
HahoAuthor Commented:
hi,
   There is one catch.. by doin it your method, I would have to declare a object of
type Class2....  from my (lengthy) text, I was trying not to do that as I would not be able to creat a global object of Class2.. can I reference the object instance of Class2 and get the value??? < pls. refer to my question>

Thanks!!! And.. I will try to clear any questions u might have cause I know my initial text isn't very user-friendly... :)

David Chong

0
 
amvCommented:
I'm not quite sure in right understanding your question, but to declare a global variable or pointer to class and to have access to it from different modules you can use such method:
in the one of the modules declare a global pointer to class like this.
CClass2 *pcclass2;
to access to this pointer from another modules you shuld define this pointer using <extern> keyword like this.
extern CClass2 *pcclass2;
and when your application creates CClass2 object you should initialise the pcclass2 like this.
cclass2.Create(<some parameters>);
pclass2 = &cclass2;
(i suppuse that exist a global class variable such as:
CClass cclass2;)
You can mail me directly to:amv@online.tomsk.net
hope this help, Mikle.
0
 
HahoAuthor Commented:
Nope,
    :) Let me try to rephrase my confusing question...
I want to access a var of CClass from CDialog by way of pointer
but I don't want to explicitly declare any global varaible
of CClass ... What I want is a method a retrieve a var's value
through the CClass object instance that is running...
I think somthing like MyClass::GetSomeFunction....  vaguely :)

which in this case does not use a local/global variable
BUT accesses the object instance/copy....

So I am reopening my question so u / or other experts can
share your view/knowledge on this. Thanks!

Bye@@ !!

David Chong

   
0
 
jaimereedCommented:
From what I understand you want the CDialog to have a pointer to CClass as a member variable but you cannot have the Class.h file in the Dialog.h file.

You CAN declare objects of type CClass in the CDialog .H file by using:

class CClass;     // Put BEFORE you use CClass

This predeclares the class and the compiler uses a placeholder in that header. In the CPP file you should then include the real CClass .H file.

As you only have one instance I suggest that you make this instance of your Application class so that you can always access it uing AfxGetApp, e.g.
( (CMyApp*) AfxGetApp() ) -> m_theClass

or some nice function...
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now