+ Reply to Thread
Results 1 to 3 of 3

Thread: Convert class to integral datatype.

  1. #1

    Convert class to integral datatype.

    How to convert class to int datatype?

    Please help.

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    You can use 'operator' keyword and overload integral type in the class.

    Example:

    Code:
    class CClassToInt
    {
    private:
    	int m_nData ;
    
    public:
    	CClassToInt(int nData)
    	{
    		m_nData = nData ;
    	}
    
    	operator int()
    	{
    		return m_nData ;
    	}
    } ;
    
    void UseClassToInt()
    {
    	CClassToInt objClassToInt(42) ;
    	int nData = objClassToInt ;
    }
    Above example converts class type to integer type.

  3. #3
    Wow, great example.

+ Reply to 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