Solved

How to access values of one class variables to another class ?

Posted on 1998-09-22
10
177 Views
Last Modified: 2010-04-01
hi ,
How to access "values" of a class variables into a non class function(eg. callback function).

THANKS TO ALL EXPERTS !!

0
Comment
Question by:kashif063098
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1173446
There are several ways.

1).  Make the data members ("values") public.  But this means that any code anywhere can access the members.  That may be a bad idea, it depends.

2)  Declare the (callback) function to be a friend of the class.  As a friend it can access private members (data members or function members) of the class.  But this is a pain if you have lots of functions that need this sort of access.

3)  Add a public member access function to the class.  This function or functions should return the current value of the data member or members.  You may also need functions to set the data member.  This tends to be the prefered way to handle this, but it is less efficient than the others.

let me know if you have questions or need more details.
0
 

Author Comment

by:kashif063098
ID: 1173447
hi neiotd,
The problem is i don't get the values of the public variable(s) outside the class instead it gives me uninitialized variable(but i am expecting a value).i hope you understand what i am trying to say.

suppose

//demo.h
class CDemo : public CDialog
{
public:
   CDemo pParent = NULL);      // standard constructor
   
   int ChannelHandles[24]  = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
   friend UINT MyThreadCallBack(LPVOID);
private:
   int ProcessTheCall(int index);
}



//Demo.cpp
UINT MyThreadCallBack(LPVOID n);
{
  ProcessTheCall(int index)
}
// in source code file
int CDemo ::ProcessTheCall(int index)
{
  if (ChannelHandles[1] = 2){//Over here the value of ChannelHandles[1] is garbage
   DoSomething();
  }

}


0
 
LVL 22

Expert Comment

by:nietod
ID: 1173448
This is FULL of errors.  There is no way it can give the wrong results--it can't compile!

here are some of the problems.  (There may be more):

 

 //demo.h
class CDemo : public CDialog
 {
public:
   CDemo pParent = NULL); // standard constructor
/// This not a constructor.  Also, there is an unpaired parentheseis
         
         int ChannelHandles[24]  = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
//// A class may not intialize an array member within the class defintion like this.
//// The inititalization must be done in the constructor.
         friend UINT MyThreadCallBack(LPVOID);
      private:
         int ProcessTheCall(int index);
      }



      //Demo.cpp
      UINT MyThreadCallBack(LPVOID n);
      {
        ProcessTheCall(int index)
//// ProcessTheCall() is a member function, it must be called for a particular object.
      }
      // in source code file
      int CDemo ::ProcessTheCall(int index)
      {
        if (ChannelHandles[1] = 2){//Over here the value of ChannelHandles[1] is garbage
         DoSomething();
        }

      }


Can you post the actual code?  or e-mail it to me at nietod@theshop.net.  Make sure the code compiled or if not, let me know what the error is.
0
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!

 
LVL 8

Expert Comment

by:Answers2000
ID: 1173449
1. Don't MyThreadCallBack a member (or make it a static member)

2. When you setup MyThreadCallBack, pass it the "this" pointer for the current CDemo object

3.
UINT MyThreadCallBack(LPVOID n);
{
  CDemo * ptrDemo = (CDemo *)n ;
  // don't know how you setup index - that's upto you
  ptrDemo->ProcessTheCall( index ) ;
}

ProcessTheCall needs to be a friend of CDemo if it's not a static member, OR make ProcessTheCall a public member of CDemo.
 


0
 

Author Comment

by:kashif063098
ID: 1173450
your solution did not work for me but the solution Answers2000 described worked for me.
thanks any way.

0
 

Author Comment

by:kashif063098
ID: 1173451
hi Answers2000
i liked your answer and it worked, so please answer this question so that i can give you the points.

THANKS TO ALL !@!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173452
Well, we were answering different questions.  My answer was for what you asked originally, his was in response to the source code.
0
 

Expert Comment

by:hawkett092198
ID: 1173453
I think nietod gave a pretty good description of both solutions to the posted questions, and helpfully pointed out the plethora of problems in the posted code.

Obviously the posted code was not the code you compiled to observe uninitialised ChannelHandles[], (you cannot compile code with unmatched parentheses for a start, or call a member function without an object for the function to act on).  Try posting the actual code - if its proprietry or IP modify enough to stuff to make it safe, but keep it readable.  Glad you got the answer you were after,

                        Colin
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173454
thanks.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1173455
>> hi Answers2000
>> i liked your answer and it worked, so please answer this question so that i can give you the points.  

whatever

0

Featured Post

Independent Software Vendors: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

695 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