operator int()

This is a discussion on operator int() within the C++ Programming forums, part of the General Programming Boards category; the book im reading has an exercise to make a class Int and overload the basic operators. there is 2 ...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    operator int()

    the book im reading has an exercise to make a class Int and overload the basic operators. there is 2 things i dont understand. it asks for bounds checking, but i dont know why the answer in the back of the book needs to convert it to long double. the other thing i dont get is 'operator int()'. when does this come in play in the class? its a conversion from Int to type int, i know that much. but when is this operator called. thanks in advance for responses
    Code:
    //////////////////////////class Int/////////////////////////
    class Int
    {
    private:
    	int i;
    public:
    	Int(): i(0)  //set default value (constructor)
    	{	}
    
    	Int(int integer):	i(integer)	  //1-arg constructor
    	{	}
    	
    	void put_int()			//display int
    	{ cout << i << endl; }
    
    	void get_int()	//get int
    	{ cin >> i; }
    
    	operator int()   //conversion operator from Int to int
    	{	return i;	}
    
    	Int operator + (Int i2)
    	{	return limit_check(long double(i) + long double(i2));	}
    
    	Int operator - (Int i2)
    	{	return limit_check(long double(i) - long double(i2));	}
    
    	Int operator * (Int i2)
    	{	return limit_check(long double(i) * long double(i2));	}
    
    	Int operator / (Int i2)
    	{	return limit_check(long double(i) / long double(i2));	}
    
    	Int limit_check(long double num)
    	{
    		if(num > 2147482648.0L || num < -2147482648.0L)
    		{
    			cout << "\nOverflow error:  Exiting." << endl;
    			exit(1);
    		}
    
    		return Int(int(num));
    	}
    
    };
    
    /////////////////////////////////////////////////////////////
    Last edited by xion; 06-15-2004 at 04:00 AM.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    when is this operator called
    It is called when a variable of type int is expected. For example:
    Code:
    void func(int nParam)
    {
    }
    
    int main()
    {
        Int a=5;
    
        func(a);
    }
    In this case 'a' will call operator int() to resolve itself to type 'int'. Quite ingenious really.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>Int a=5;
    int a=5;

    Geez.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Maybe you misunderstand, XSquared. Int is a class.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Whoops. You're right. Name your clases differently, geez!
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    Quote Originally Posted by bennyandthejets
    In this case 'a' will call operator int() to resolve itself to type 'int'. Quite ingenious really.
    thanks for the reply.
    why is it required to convert it to long double and check the bounds?

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Name your clases differently, geez!
    I agree. I would have called it CInt.

    why is it required to convert it to long double and check the bounds?
    I don't know, it does seem a bit pointless doesn't it.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21