+ Reply to Thread
Results 1 to 2 of 2

Thread: How can I convert my class object to a bool type?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    43

    How can I convert my class object to a bool type?

    Following code does not compile.

    How to resolve this issue? Basically, I want to convert class object to bool type.

    Code:
    class CVerify
    {
    private:
    	bool m_Value ;
    
    public:
    	CVerify(bool Value)
    	{
    		m_Value = Value ;
    	}
    }  ;
    
    void CheckValue()
    {
    	CVerify myValue(false) ;
    
    	if(!myValue)
    	{
    		OutputDebugString("false\n") ;
    	}
    }

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    27
    Use cast operator for this.

    Below example may help you:

    Code:
    class CVerify
    {
    private:
    	bool m_Value ;
    
    public:
    	CVerify(bool Value)
    	{
    		m_Value = Value ;
    	}
    
    	operator bool()
    	{
    		return m_Value ;
    	}
    }  ;
    
    void CheckValue()
    {
    	CVerify myValue1(false) ;
    
    	if(!myValue1)
    	{
    		OutputDebugString("false\n") ;
    	}
    
    	CVerify myValue2(true) ;
    
    	if(myValue2)
    	{
    		OutputDebugString("true\n") ;
    	}
    }

+ 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