Solved

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

Posted on 1998-09-22
10
169 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
your solution did not work for me but the solution Answers2000 described worked for me.
thanks any way.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:kashif063098
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
Comment Utility
>> 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now