How to set a "default" return value for a class?

This is a discussion on How to set a "default" return value for a class? within the C++ Programming forums, part of the General Programming Boards category; How do I set what an object returns when just the object name is used? For instance, a string will ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    28

    How to set a "default" return value for a class?

    How do I set what an object returns when just the object name is used? For instance, a string will return a value when just the name is used, without using any particular function. Is this done by overloading?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You mean something like this?
    Code:
    #include <iostream>
    
    class Something
    {
    	public:
    		operator char(){
    			return 'c';
    		}
    };
    
    void someFunction(char val)
    {
    	std::cout<<val;
    }
    
    int main()
    {
    	Something s;
    	
    	someFunction(s);
    	
    	std::cin.get();
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Be careful with that, though. While it seems cool, it can often lead to unintended code compiling but not working as expected. It is usually better to just go ahead and make a named function.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    Ill try that and see if I run into any problems, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM

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