Solved

Getting a pointer to a value?

Posted on 1998-08-14
5
158 Views
Last Modified: 2013-11-20
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
Comment
Question by:Haho
  • 2
  • 2
5 Comments
 

Expert Comment

by:amv
Comment Utility
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
 
LVL 1

Author Comment

by:Haho
Comment Utility
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
 

Expert Comment

by:amv
Comment Utility
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
 
LVL 1

Author Comment

by:Haho
Comment Utility
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
 

Accepted Solution

by:
jaimereed earned 70 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now