Solved

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

Posted on 1998-09-22
10
171 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now