CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2000
    Location
    Tehran - Iran
    Posts
    949

    Initialing member variable from another object ( class ) ...

    Hi,

    I want to initial a member variable of my dialog from an object of other class ...
    Maybe below sample could show what i want to say :

    Code:
    	class CSample
    	{
    		public: 
    			int * m_pN;
    			
    			void SetVariable (int * n)
    			{
    				m_pN = n;
    			}
    			
    			void InitVariable()
    			{
    				m_pN = new int (5);
    			}
    	};
    
    
    BOOL CtestmapDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	
    // m_pNumber is defined as: int *
            
            CSample obj;
    	obj.SetVariable( m_pNumber );
    	obj.InitVariable();
    
            return TRUE;
    }
    the code compiles fine but m_pNumber doesnt initialed ...
    What should i do to initial the m_pNumber externally ?!

    Regards,
    Hadi

  2. #2
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Initialing member variable from another object ( class ) ...

    Code:
    	class CSample
    	{
    		public: 
    			int * m_pN;
    			
    			void SetVariable (int * n)
    			{
    				m_pN = n;
    			}
    			
    			void InitVariable()
    			{
    				m_pN = new int (5);
    			}
    	};
    
    
    BOOL CtestmapDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	
    // m_pNumber is defined as: int
            
            CSample obj;
    	obj.SetVariable( &m_pNumber );
    	obj.InitVariable();
    
            return TRUE;
    }
    Try to made this changes...

  3. #3
    Join Date
    Oct 2000
    Location
    Tehran - Iran
    Posts
    949

    Re: Initialing member variable from another object ( class ) ...

    juanpast, Thanks for reply ...
    But it doesnt work either ... anyway i want to use pointers instead of references ...

    Regards,
    Hadi

  4. #4
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Initialing member variable from another object ( class ) ...

    I write the code and woks fine....
    Attached Files Attached Files

  5. #5
    Join Date
    Oct 2000
    Location
    Tehran - Iran
    Posts
    949

    Re: Initialing member variable from another object ( class ) ...

    Hi Juan,

    I think u got the question wrong ...
    It's not about Array , right ?!
    m_pN just initialed to have 5 ...

    Simply, i'm looking for a way to point m_pNumber to m_pN !
    When m_pN initials , m_pNumber points to it .. ok ?

    Thanks

  6. #6
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Initialing member variable from another object ( class ) ...

    Try to explain better... I am sorry, but I do not understand you....

  7. #7
    Join Date
    May 2002
    Posts
    1,435

    Re: Initialing member variable from another object ( class ) ...

    Is this what you are trying to do?
    Code:
    class CSample
    	{
    		public: 
    			int ** m_pN;
    			
    			void SetVariable (int ** n)
    			{
    				m_pN = n;
    			}
    			
    			void InitVariable()
    			{
    				*m_pN = new int (5);
    			}
    	};
    Code:
    CSample obj;
    obj.SetVariable( &m_pNumber );
    obj.InitVariable();

  8. #8
    Join Date
    Oct 2000
    Location
    Tehran - Iran
    Posts
    949

    Resolved Re: Initialing member variable from another object ( class ) ...

    Yessss, Thank u so much ...

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Initialing member variable from another object ( class ) ...

    Do yourself a favor and initialize the variable to NULL in the constructor and put some validation on the InitVariable and SetVariable methods.

  10. #10
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Initialing member variable from another object ( class ) ...

    I think, too, that if SetVariable were called AFTER a call to InitVariable, then SetVariable should have to check and possibly delete the previous allocated contents, else there'd be a memory leak.

    Actually, the same applies to InitVariable. There's nothing that prevents either from being called multiple times.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  11. #11
    Join Date
    May 2002
    Posts
    1,435

    Re: Initialing member variable from another object ( class ) ...

    As Arjay and JVene have pointed out, the code that I have shown is VERY CRUDE and should be modified to include fairly extensive error checking. I debated whether or not to simply post the code or post it with many suggestions and decided to take things one step at a time, and hence the simple code.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Initialing member variable from another object ( class ) ...

    Understood 0xC0000005, I believe our comments were directed at the OP.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured