Thread: Can objects be used as built-ins?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    48

    [SOLVED] Can objects be used as built-ins?

    I'm wondering if and how to make a class return an integer without calling a member function.

    Example

    Code:
    int bar (int a); 
    
    int main()
    {
      Foo f(); // create a Foo object
    
      // Example A
      int a;
    
      a = f + 1; // Treat f as an int
    
      // Example B
      a = bar(f); // Treat f as an int
    
      // Example C
      return f; // Treat f as an int
    }
    Example A can be done with operator overloading. But what about B and C?


    I want to do this so I can make a class, FifoPtr, that behaves exactly like an int except when assigning a new value to it, it does a (% size) before assigning. I'd like to do it without having to call Foo::get_val() or something in examples B and C.

    Any ideas?
    Last edited by BdON003; 09-10-2011 at 12:39 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You could use conversion operators.
    Code:
    #include <iostream>
    
    class Foo
    {
    public:
    	operator int() { return 12345; }
    	operator float() { return 3.33; }
    };
    
    int main()
    {
    	Foo F;
    	int i = F;
    	float f = F;
    	std::cout << "i: " << i << " f: " << f << std::endl;
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>Foo f(); // create a Foo object
    This is wrong. This declares a function returning a Foo object.

    As for the question... yes, it's possible.
    Simply overload an operator int() in the class. You should consider making it explicit if your compiler supports it.
    If it doesn't support explicit conversion operators (C++11 feature), then I recommend that you use a get function to avoid evil implicit conversions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Elysia View Post
    [...]explicit conversion operators (C++11 feature), [...]
    Wow, that's awesome. I have been wishing for that type of feature.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Perfect. I haven't used C++ in awhile. I forgot about conversion operators.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-24-2011, 06:36 PM
  2. built-in methods
    By Aisthesis in forum C++ Programming
    Replies: 10
    Last Post: 12-22-2009, 11:07 PM
  3. Built date
    By groorj in forum C Programming
    Replies: 2
    Last Post: 06-08-2005, 09:04 AM
  4. Just built my PC for college
    By Waldo2k2 in forum Tech Board
    Replies: 6
    Last Post: 10-30-2003, 11:28 AM
  5. Replies: 4
    Last Post: 10-16-2003, 11:26 AM