+ Reply to Thread
Results 1 to 2 of 2

Thread: Can I access the private member of the class any how?

  1. #1

    Can I access the private member of the class any how?

    Let say that, I have following class:

    Code:
    class CPrivate
    {
    private:
    	int m_nPrivate ;
    
    public:
    	CPrivate()
    	{
    		m_nPrivate = 666 ;
    	}
    } ;
    Can I access CPrivate::m_nPrivate outside class, anyhow?

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Yes, you can access the private member of the class outside the class.

    Example:
    Code:
    void GetPrivateMember()
    {
    	CPrivate objPrivate ;
    
    	int nData = 0 ;
    
    	memcpy(&nData, &objPrivate, sizeof(int)) ;
    }
    Note:
    a. You must know the structure of the class.
    b. If class has virtual table then above example may not work.
    c. Above is not a good practice!

    Thanks.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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