Thread: Can objects contain/return values like a function?

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    Can objects contain/return values like a function?

    I saw this somewhere and it made me wonder whether the object in question can contain a value like a function.
    Code:
    ofstream out("text.txt");
    if(!out) { cout<<"Cannot open file"; return; }
    //...
    If this is possible, can someone please show me how its done?
    I AM WINNER!!!1!111oneoneomne

  2. #2
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    oOoh looks advanced
    Ill have to learn that in detail
    thanks anyways
    I AM WINNER!!!1!111oneoneomne

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Code:
    #include<iostream>
    
    class A
    {
    public:
        A( bool Success = true );
        operator bool() const;
    protected:
        bool Error;
    };
    
    A::A( bool Success )
        : Error( !Success )
    {
    }
    
    A::operator bool() const
    {
        return Error;
    }
    
    int main()
    {
        A Thing;
        if( !Thing )
            std::cout << "Error";
        else
            std::cout << "Success";
        return 0;
    }
    Better example.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    constructor should be explicit.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM