How to use the variables in another class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: How to use the variables in another class

  1. #1
    Join Date
    Aug 2011
    Posts
    21

    Arrow 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 05:21 AM.

  2. #2
    Join Date
    Jun 2010
    Posts
    115

    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.

  3. #3
    Join Date
    Aug 2011
    Posts
    21

    Re: How to use the variables in another class

    Quote Originally Posted by Maejie View Post
    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 !

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,287

    Re: How to use the variables in another class

    Quote Originally Posted by Angela2010 View Post
    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();

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: How to use the variables in another class

    Quote Originally Posted by Angela2010 View Post
    I have tried some methods , but failed ,well ,thank you all the same !
    Wou are welcome!
    How you tried, how they failed?
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    Join Date
    May 2011
    Location
    India
    Posts
    18

    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 ..

  7. #7
    Join Date
    May 2011
    Location
    India
    Posts
    18

    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 ?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: How to use the variables in another class

    Quote Originally Posted by ManishaSantosh View Post
    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 05:58 AM.

  9. #9
    Join Date
    May 2011
    Location
    India
    Posts
    18

    Smile Re: How to use the variables in another class

    Thankx Paul ..

  10. #10
    Join Date
    Aug 2011
    Posts
    21

    Re: How to use the variables in another class

    Quote Originally Posted by Paul McKenzie View Post
    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

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,972

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center