Link to home
Start Free TrialLog in
Avatar of kashif063098
kashif063098

asked on

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 !!

Avatar of nietod
nietod

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.
Avatar of kashif063098

ASKER

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();
  }

}


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.
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.
 


your solution did not work for me but the solution Answers2000 described worked for me.
thanks any way.

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 !@!
Well, we were answering different questions.  My answer was for what you asked originally, his was in response to the source code.
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
thanks.
ASKER CERTIFIED SOLUTION
Avatar of Answers2000
Answers2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial