Class object as condition?

This is a discussion on Class object as condition? within the C++ Programming forums, part of the General Programming Boards category; Hi! What happens if you have an object my_obj of class my_class , and you write: Code: if (my_obj) {...} ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Class object as condition?

    Hi!

    What happens if you have an object my_obj of class my_class, and you write:

    Code:
    if (my_obj) {...} else {...}
    How do you know which code block that is executed? The first or the second? Can you make my_class decide what value an object of that type should have when it is looked at as a boolean variable (here true can be any value other than 0 so it doesn't have to be of the type bool)?

    For example, I have a class which is basically a kind of wrapper class for a variable v (it contains some other stuff too). When I write if(obj) I want it to be the same as if I would have written if(obj.v).

    Thanks in advance!
    Come on, you can do it! b( ~_')

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Your best bet is to go ahead and give a meaningful name to the test, even if it's just is_valid(). This makes things easier to read and maintain. If you really want to have that magic test, write a conversion operator from bool.
    Code:
    operator bool() const {return v;}

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    The most typical way to handle it is with operator void*:

    Code:
    class Myclass {
    public:
       operator void* () const
          {
          return (isThisObjectValid)? (void*)this : 0;
          }
    };
    Using operator void* prevents nonsensical operations that may arise when you use operator bool (Like doing Myclass() + Myclass()).

    This has a few corner cases where such an approach may cause problems (IE: deleting an instance of Myclass), in which case you may want to read on the safe bool idiom. Using operator void* should suffice in most situations, though.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    You can also return a member pointer. That cannot be deleted.

    Code:
    class MyClass {
    private:
       bool isThisObjectValid;
       void nop();
    public:
       typedef  void (MyClass::*MyClassFn)(); 
       operator MyClassFn () const
          {
          return (isThisObjectValid)? &MyClass::nop : 0;
          }
    };
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by King Mir
    You can also return a member pointer. That cannot be deleted.
    That comes one step closer towards the safe bool idiom. The next step would be to disable comparison operators for MyClass if they do not make sense.

    Anyway, I am still waiting for explicit conversion functions to finally arrive
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by King Mir View Post
    You can also return a member pointer. That cannot be deleted.

    Code:
    class MyClass {
    private:
       bool isThisObjectValid;
       void nop();
    public:
       typedef  void (MyClass::*MyClassFn)(); 
       operator MyClassFn () const
          {
          return (isThisObjectValid)? &MyClass::nop : 0;
          }
    };
    King Mir, can you please explain the code? First you typedef MyClassFn as a pointer to a void function taking no arguments (I guess?). Is it MyClassFn that is returned when you use an object of MyClass in an if-statement, since it's a void function? Or does it return a pointer to a void function (I'm confused)? And why do you return nop, what's it good for? Is all this code really necessary?
    Come on, you can do it! b( ~_')

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    MyClassFn is a pointer to member of MyClass that points to a method that takes void function taking no arguments, except the implicit this pointer. What matters it that it is the type of a pointer to nop.

    MyClassFn used the type of the conversion operator. It is the same as void* in Ronix's example or bool in grib's. The actual value returned is NULL in the case of invalid, or a pointer to the method nop in the case of valid. Any non static method can be used in place of nop, provided that the type is made to match the typedef.

    I tried to find a way to do this without the typedef, but could not get it to compile.

    Every line serves a necessary function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

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. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

Tags for this Thread


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