Solved

Beginner: sharing information between classes

Posted on 2000-05-05
10
838 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Accepted Solution

by:
abdij earned 70 total points
Comment Utility
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
Comment Utility
thanks for the many solutions i like  abdij´s proposal most . How is "friend" working ? Can you give little example ?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
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
Comment Utility
Adjusted points from 50 to 70
0
 

Author Comment

by:peter_b
Comment Utility
"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
Comment Utility
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
Comment Utility
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

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

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

728 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

13 Experts available now in Live!

Get 1:1 Help Now