Thread: New bool class... Question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    New bool class... Question

    Does this look like it will serve its purpose well. I just hate that the C++ bool can't be used in containers so I decided to write my own that is more C++ish, and that is safe for containers. Tell me if i am missing anything.

    I realize most of you will think this is pointless if not all of you, but I don't #define for stuff like this, and I think this will work better for my purposes.
    Code:
    class Bool
    {
    public:
    	//Default constructor
    	Bool(unsigned char initVal=0)
    	{
    		if ( initVal == Bool::TRUE )
    			ThisBool = new unsigned char(0);
    		if ( initVal >= Bool::FALSE || initVal == Bool::FALSE)
    			ThisBool = new unsigned char(1);
    	};
    
    	//Copy constructor
    	Bool(const Bool& copy)
    	{
    		if ( copy.ThisBool )
    		{
    			this->ThisBool = new unsigned char(*copy.ThisBool);
    			delete copy.ThisBool;
    		}
    		else
    		{
    			this->ThisBool = new unsigned char(Bool::TRUE);
    		}
    	};
    
    	//default destructor
    	~Bool ()
    	{
    		delete ThisBool;
    	};
    
    	//Enumeration for Bool values
    	enum TYPE
    	{
    		TRUE = 0,
    		FALSE = 1
    	};
    
    	//Duh?
    	const bool True()
    	{
    		if ( *ThisBool == Bool::TRUE )
    			return true;
    		else
    			return false;
    	};
    
    	//Duh?
    	const bool False()
    	{
    		if ( *ThisBool == Bool::FALSE )
    			return true;
    		else
    			return false;
    	};
    
    	//Get the value of the Bool without changing it
    	const unsigned char& Get()
    	{
    		return *ThisBool;
    	};
    
    	//Get the value of the Bool without changing it
    	const unsigned char& operator*()
    	{
    		return *ThisBool;
    	};
    
    	//Toggle the value of the Bool
    	const unsigned char& Toggle()
    	{
    		if ( *ThisBool == Bool::TRUE )
    		{
    			*ThisBool = Bool::FALSE;
    			return *ThisBool;
    		}
    		else
    		{
    			*ThisBool = Bool::TRUE;
    			return *ThisBool;
    		}
    	};
    
    	//Set the value of the Bool
    	const unsigned char& Set(TYPE new_val)
    	{
    		*ThisBool = new_val;
    		return *ThisBool;
    	};
    private:
    	unsigned char* ThisBool;
    };
    EDIT: Added operator*() function. Little easier to type, little harder to read(understand). Oh well.
    Last edited by Raigne; 04-03-2008 at 11:06 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Using dynamic memory allcation for this is downright ludicrous! It's not like you're ever shallow copying here.
    If you simply want to avoid the vector<bool> specialisation, then wrapping a bool in a class where you only add a couple of operators, should be more than enough.
    'bool' can be used in containers anyway, it's only the vector that does funky stuff with it that stops a couple of things from working.

    The best thing you can do with the posted code is delete it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It's not like you're ever shallow copying here.
    Or rather, it's not like you're ever deep copying here.

    'bool' can be used in containers anyway, it's only the vector that does funky stuff with it that stops a couple of things from working.
    I agree. It depends on what exactly you want, but something like this could suffice:
    Code:
    class Bool
    {
    public:
        Bool(bool value = false) : value(value) {}
        operator bool() { return value; }
    private:
        bool value;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    Or rather, it's not like you're ever deep copying here.


    I agree. It depends on what exactly you want, but something like this could suffice:
    Code:
    class Bool
    {
    public:
        Bool(bool value = false) : value(value) {}
        operator bool() { return value; }
    private:
        bool value;
    };
    Oh yeah, heck it's neither shallow copying, nor deep copying. It's worse than I thought! The copy-constructor destroys the item being copied from, such that the destructor of the item being passed in will then throw, causing a fatal crash.

    What you just posted is exactly what I had in mind.
    Last edited by iMalc; 04-04-2008 at 01:49 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Raigne View Post
    I just hate that the C++ bool can't be used in containers
    Says who? The vector<bool> mistake is the only issue I know of with bools, but even that isn't really that bad. I haven't even had a need for a vector<bool> yet, so I doubt it's worth the time & effort to create a fix for it.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you must make your own class, at least when you perform construction like this:
    Code:
    	Bool(unsigned char initVal=0)
    	{
    		if ( initVal == Bool::TRUE )
    			ThisBool = new unsigned char(1);
                    else if ( initVal == Bool::FALSE)
    			ThisBool = new unsigned char(0);
                    else 
                             std::cerr << "Invalid truth value << " (int) initval << std::endl;
    	};
    Don't turn truth values upside down: standard is that TRUE is anything but zero, and FALSE is zero exactly.
    Code:
    	enum TYPE
    	{
    		TRUE = 0,
    		FALSE = 1
    	};

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you really need a Bool class, wouldn't something like this be a lot better and simpler:
    Code:
    class Bool
    {
    public:
    	Bool() : m_Bool( bool() ) {}
    
    	Bool( bool  bVal ) : m_Bool( bVal ) {}
    
    	operator bool() const
    	{
    		return m_Bool;
    	}
    
    	Bool& operator=( const Bool&  rhs )
    	{
    		m_Bool = rhs.m_Bool;
    		return *this;
    	}
    
    private:
    	bool	m_Bool;
    };

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and as laserlights post shows, you don't even need an assignment operator.

    However, if you want to make sure that it actually returns a bool:
    Code:
    	operator bool() const
    	{
    		return !!m_Bool;
    	}

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Yes, and as laserlights post shows, you don't even need an assignment operator.

    --
    Mats
    Oops, I didn't even see that.

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Takes a lot of work to get a truth statement from Raigne's class. Reminds me of my last relationship...

    *ba dum ching*

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    However, if you want to make sure that it actually returns a bool:
    That seems unnecessary, since operator bool(), by definition, returns a bool. That said, I did neglect to make operator bool() const in my example, so that fix should be incorporated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    How does it take a lot to get a truth statement from my class?
    Code:
    Bool Test(Bool::TRUE);
    if ( Test.True() )
    I really see no work to that.
    Maybe I just like over complicating things. If that is the case I will just have to stop asking you guys for help. Since my ways are always "ludicrous". Have fun, and ty.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I really see no work to that.
    Everything beyond if(Test) is a lot.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Since my ways are always "ludicrous".
    You realize that only one person referred to anything as ludicrous, and that was just the use of dynamic memory allocation.

    Your class was a nice attempt but there are many issues with it that have been pointed out. Is that not what you wanted?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe I just like over complicating things. If that is the case I will just have to stop asking you guys for help. Since my ways are always "ludicrous". Have fun, and ty.
    Well, you wanted us to tell you if you were missing anything. We told you that you missed a more straightforward implementation, and that you missed a copying bug in your current implementation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 01:43 AM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Simple Short Class Question
    By Travis Dane in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2002, 07:12 PM