Solved

Getting a pointer to a value?

Posted on 1998-08-14
5
160 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
ID: 1320849
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
ID: 1320850
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
ID: 1320851
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
ID: 1320852
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
ID: 1320853
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 78
bobThere java chalenge 45 92
string initialization in java 11 113
viewing source code from eclipse 13 105
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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