Solved

Getting a pointer to a value?

Posted on 1998-08-14
5
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shell script to login to remote m/c and execute commands 10 81
ShiftLeft challenge 21 95
FizzBuzz challenge 9 87
Excel file not created as expected 7 110
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

739 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