-
December 26th, 2011, 08:47 PM
#1
How to use the variables in another class
Hi all,
I have class CTestView and CChartDlg , I want to use an array of CTestView in the other class, here is the code:
Code:
class CTestView : public CView
{
...........................
public:
int num[10]
void fun();
...................................
};
.........................
void CTestView::fun()
{
................
for (int i=0;i<10;i++)
num[i]=i;
.........
}
the other class:
Code:
class CChartDlg : public CDialog
{
public:
CChartDlg(CWnd* pParent = NULL);
.......................
protected:
afx_msg void OnCreate();
.........................
}
...............................
void CChartDlg::OnCreate()
{ ...............
// I want to use num[0]~num[9] in this function.
}
I want to use num[0]~num[9] in class CChartDlg
I tried CCentriodView getv;
but getv.num[0] is not the right num ,can anyone help me with this?Thank you !
Last edited by Angela2010; December 27th, 2011 at 06:21 AM.
-
December 26th, 2011, 08:56 PM
#2
Re: How to use the variables in another class
I really want to help but I still am jobless after all . Don't be that dependent on forums messages.
-
December 26th, 2011, 09:05 PM
#3
Re: How to use the variables in another class
Originally Posted by Maejie
I really want to help but I still am jobless after all . Don't be that dependent on forums messages.
I have tried some methods , but failed ,well ,thank you all the same !
-
December 27th, 2011, 01:38 AM
#4
Re: How to use the variables in another class
Originally Posted by Angela2010
I have tried some methods , but failed ,well ,thank you all the same !
Do you invoke CChartDlg from CTestView? If so, pass in a pointer of CTestDlg to CChartDlg.
Code:
CChartDlg dlg(this);
dlg.DoModal();
-
December 27th, 2011, 10:52 AM
#5
Re: How to use the variables in another class
Originally Posted by Angela2010
I have tried some methods , but failed ,well ,thank you all the same !
Wou are welcome!
How you tried, how they failed?
-
December 28th, 2011, 04:49 AM
#6
Re: How to use the variables in another class
Hi , I am also trying for the same. I need to use a same structure in two different files and in two independant classes ...
May I know the method ?
Last option will be declaring the struct in both the files ..
-
December 28th, 2011, 06:13 AM
#7
Re: How to use the variables in another class
I have made a common header file .. but I feel that is a C prog method. Should I define a class and use Friend operation ?
-
December 28th, 2011, 06:53 AM
#8
Re: How to use the variables in another class
Originally Posted by ManishaSantosh
I have made a common header file .. but I feel that is a C prog method.
Then why does C++ have header files such as <iostream>, <vector>, <string>, <map>, etc.? What purpose do they serve?
I need to use a same structure in two different files and in two independant classes
So use a real life example. What if you want to use the class "string" in two independent classes, how do you do that?
Code:
// File1.cpp
#include <string>
class C1
{
std::string s;
};
Code:
// File2.cpp
#include <string>
class C2
{
std::string s;
};
I just used string in two independent classes and in two different files.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 28th, 2011 at 06:58 AM.
-
December 28th, 2011, 06:55 AM
#9
Re: How to use the variables in another class
-
December 28th, 2011, 09:32 PM
#10
Re: How to use the variables in another class
Originally Posted by Paul McKenzie
I just used string in two independent classes and in two different files.
Hi Paul,
I tried your method , but it did not work in my case, can you help me with this?thank you
-
December 29th, 2011, 12:19 AM
#11
Re: How to use the variables in another class
I want to use num[0]~num[9] in class CChartDlg
I tried CCentriodView getv;
but getv.num[0] is not the right num ,can anyone help me with this?
What the try does mean? You defined getv in the function? Of course it won't work, as you need to access already existing view object, but you create a new one.
So, to make the object be accessible in another object you need to pass the object address (pointer) to that other object. BTW, if I get this correct, you already should have the view pointer in the dialog as dialog parent. If so,
Code:
void CChartDlg::OnCreate()
{
CTestView* pView = (CTestView*)GetParent();
// I want to use num[0]~num[9] in this function.
int num0 = pView->num[0];
}
Best regards,
Igor
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|