Solved

Getting a pointer to a value?

Posted on 1998-08-14
5
165 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

634 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