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

hi ,
How to access "values" of a class variables into a non class function(eg. callback function).

THANKS TO ALL EXPERTS !!

kashif063098Asked:
Who is Participating?
 
Answers2000Connect With a Mentor Commented:
>> hi Answers2000
>> i liked your answer and it worked, so please answer this question so that i can give you the points.  

whatever

0
 
nietodCommented:
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
 
kashif063098Author Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
nietodCommented:
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
 
Answers2000Commented:
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
 
kashif063098Author Commented:
your solution did not work for me but the solution Answers2000 described worked for me.
thanks any way.

0
 
kashif063098Author Commented:
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
 
nietodCommented:
Well, we were answering different questions.  My answer was for what you asked originally, his was in response to the source code.
0
 
hawkett092198Commented:
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
 
nietodCommented:
thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.