Solved

Beginner: sharing information between classes

Posted on 2000-05-05
10
840 Views
Last Modified: 2008-03-06
win32, Visual C++6 , beginner (i mean real beginner :)

Sorry for this question but i really dont know. I am getting the time in one class and i am trying to access it in another class. But how do i do that ?

void CW1Dlg::SetData(float* newnndata, float* newppdata)
{

      chart3.SetData(newnndata);
      chart2.SetData(newppdata);
      CTime lastTime;
       lastTime = CTime::GetCurrentTime();



}

now i would like to access lastTime elsewhere. For example in another class

CTimeSpan MinutesX15 = CTimeSpan(0,0,1,0);
      CString display_time;
      CTimeSpan Minutesmin15 = CTimeSpan(0,0,15,0);
      lastTime -= Minutesmin15;

like this.

Can you give me some tips with sharing information in a program. I know its really basic but it would help me understand.


thanks...
0
Comment
Question by:peter_b
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 2779883
How about having one function in the CW1Dlg which returns you the address of lastTime. Then you send that address to the other class which then can use that address. Only a few changes have to be made (ie, pointers and references), but you can do that on your own.

0
 

Expert Comment

by:forza
ID: 2779940
Use the "friend" keyword to let the CW1Dlg class and the other class communicate.
Alternatively you could make lastTime global, it will work, but normally I try to avoid this...
0
 
LVL 1

Expert Comment

by:ch_vasu
ID: 2780149
Declare one more member function in CW1Dlg class as
public:
CTimeSpan GetLastTime();
void SetLastTime(CTimeSpan cTime);

then define as given below

CTimeSpan CW1Dlg::GetLastTime()
{
      return lastTime;
}
void CW1Dlg::SetLastTime(CTimeSpan cTime )
{
      lastTime = cTime;
}

To get/read the Last time use

CW1Dlg cW1DlgTemp;
CTimeSpan dtTempTime = cW1DlgTemp.GetLastTime();

To Set/Update the Last time use

CTimeSpan MinutesX15 = CTimeSpan(0,0,1,0);
CTimeSpan Minutesmin15 = CTimeSpan(0,0,15,0);

CTimeSpan dtTempTime = cW1DlgTemp.GetLastTime();

dtTempTime -= Minutesmin15;

cW1DlgTemp.SetLastTime(dtTempTime);

Try this
All the Best
-Vasudev

 
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Accepted Solution

by:
abdij earned 70 total points
ID: 2781814
Hi,

 Vasu: Ur solution may work but is not exactly the right way to implement.
I think forza is right. Since peter has 2 different classes it is better to go by the "friend" approach. His alternate approach though should be avoided.

 Another alternative: again not the best but easy is:

Have a static member variable in CW1Dlg say m_stcTime and 2 member functions CW1Dlg::SetTime(..) and CW1Dlg::GetTime(..)
..

To set time call SetTime() and to get it anywhere call GetTime.

The limitation is that only one time can exist at any time. For example if there is a CW1Dlg dlg1 and dlg2, and both try to do set get the time the whole thing will definately be a mess. It is good if and only if one and only one instance of CW1Dlg class exists.

Hope this helps,
All the best,
bye

BYW: Is ch_vasu from Kshema Tech, Blr????

Bye
Abdij
0
 

Author Comment

by:peter_b
ID: 2782010
thanks for the many solutions i like  abdij´s proposal most . How is "friend" working ? Can you give little example ?
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2782038
Just a thought: Friend is a bad programming practice when trying to program in an object-oriented language, that should be avoided.  Providing a getTime and setTime method is the best way to go about it to maintain code that's easy to use and maintain.

Laminamia :)
0
 

Author Comment

by:peter_b
ID: 2783429
Adjusted points from 50 to 70
0
 

Author Comment

by:peter_b
ID: 2783430
"Have a static member variable in CW1Dlg say m_stcTime and 2 member functions CW1Dlg::SetTime(..) and CW1Dlg::GetTime(..) "

could you write down an example please. I come from Java. Beginner there too and i really dont understand the syntax of those ::

i am adjusting the points for some extra lesson if you dont mind ?
thanks...
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 2783515
If you have a class with all kinds of functions in it. You have to somehow fill in the functions (what they should do), thus you write out the functions like

void GetTime(void)
{
   ...
}

but how will the compiler know that it belongs to that certain class? It knows by using the double :
First you write the returntype, then the class and then you write the function name.

CJ
0
 
LVL 1

Expert Comment

by:ch_vasu
ID: 2788798
Hi Peter,

Could u pl. explain more on the context of usage of your variable 'lastTime'. Because the implementation depends upon the life time of the object and the security of data.
1. If the variable is used to store the Last time of any object of CW1Dlg then you can make use of getTime() and setTime(). But if each object of the class has its own lastdate (usaully)storage then the static usage will be wrong.
2. The usage the friend function almost voilates the O.O.Concept. Before using friend function think twise whether that context suggests that?. (i am worried about the security of lastTime)

Ans1:
if you need only static variable m_stcTime you need not have to have member function to use it. U can
1) Get the Last Date by  
CTimeSpan dtTempTime  = CW1Dlg::m_stcTime and
2) Set the Last Date by  CW1Dlg::m_stcTime = dtTempTime  and

Ans2:
if you need static variable and 2 static member functions then,

Example:
Declare members in CW1Dlg class as
public:
static m_stcTime;
static CTimeSpan GetLastTime();
static void SetLastTime(CTimeSpan cTime);

rest of the  code remains same as commented lasttime.

Try this out
bye
-Vasudev



 
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 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