adding a bool to a string

Discussion in 'C++' started by aaragon, Nov 15, 2006.

  1. aaragon

    aaragon Guest

    Hi All,

    I'm trying to print boolean values and after I searched the entire web,
    I couldn't find anything that does this =/ The code that I ended up
    using is as follows:

    string boolString(Object* obj_, size_t maxOutput_){
    string string_;

    for (size_t i=0; i<maxOutput_; i++){

    if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
    if (obj_->boolean(i) == true)
    string_ += "1";
    else
    string_ += "0";
    }
    }

    return string_;
    }

    Is there any way to simplify this code?
     
    aaragon, Nov 15, 2006
    #1
    1. Advertising

  2. aaragon

    Rolf Magnus Guest

    aaragon wrote:

    > Hi All,
    >
    > I'm trying to print boolean values and after I searched the entire web,
    > I couldn't find anything that does this =/ The code that I ended up using
    > is as follows:
    >
    > string boolString(Object* obj_, size_t maxOutput_){
    > string string_;
    >
    > for (size_t i=0; i<maxOutput_; i++){
    >
    > if( sizeof(obj_->boolean(i)) == sizeof(bool) ){


    What is the above line supposed to do? What does obj_->boolean(i) return?
    Note that sizeof is always computed at compile time.

    > if (obj_->boolean(i) == true)
    > string_ += "1";
    > else
    > string_ += "0";
    > }
    > }
    >
    > return string_;
    > }
    >
    > Is there any way to simplify this code?


    string boolString(Object* obj_, size_t maxOutput_){
    stringstream stream_;

    for (size_t i=0; i<maxOutput_; i++)
    // depending on the return type of boolean(), you can remove the cast
    stream_ << bool(obj_->boolean(i));

    return stream_.str();
    }
     
    Rolf Magnus, Nov 15, 2006
    #2
    1. Advertising

  3. aaragon wrote:
    > I'm trying to print boolean values and after I searched the entire
    > web, I couldn't find anything that does this =/ The code that I
    > ended up using is as follows:
    >
    > string boolString(Object* obj_, size_t maxOutput_){
    > string string_;
    >
    > for (size_t i=0; i<maxOutput_; i++){
    >
    > if( sizeof(obj_->boolean(i)) == sizeof(bool) ){


    Why do you need to check the size? What does it give you?

    > if (obj_->boolean(i) == true)
    > string_ += "1";
    > else
    > string_ += "0";


    This could be written

    string_ += (obj_->boolean(i) ? '1' : '0');

    which I find a bit simpler.

    Also, consider that you call 'boolean' member twice here (once to
    check the size and the other to actually get the value); if the
    function has side effects, calling it twice may not be what you
    want.

    > }
    > }
    >
    > return string_;
    > }
    >
    > Is there any way to simplify this code?


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #3
  4. aaragon

    aaragon Guest

    Victor Bazarov wrote:
    > aaragon wrote:
    > > I'm trying to print boolean values and after I searched the entire
    > > web, I couldn't find anything that does this =/ The code that I
    > > ended up using is as follows:
    > >
    > > string boolString(Object* obj_, size_t maxOutput_){
    > > string string_;
    > >
    > > for (size_t i=0; i<maxOutput_; i++){
    > >
    > > if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

    >
    > Why do you need to check the size? What does it give you?


    I need to check the size because the function may not return a bool but
    an integer for example.

    >
    > > if (obj_->boolean(i) == true)
    > > string_ += "1";
    > > else
    > > string_ += "0";

    >
    > This could be written
    >
    > string_ += (obj_->boolean(i) ? '1' : '0');
    >
    > which I find a bit simpler.
    >
    > Also, consider that you call 'boolean' member twice here (once to
    > check the size and the other to actually get the value); if the
    > function has side effects, calling it twice may not be what you
    > want.
    >
    > > }
    > > }
    > >
    > > return string_;
    > > }
    > >
    > > Is there any way to simplify this code?

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
     
    aaragon, Nov 15, 2006
    #4
  5. aaragon

    aaragon Guest

    Rolf Magnus wrote:
    > aaragon wrote:
    >
    > > Hi All,
    > >
    > > I'm trying to print boolean values and after I searched the entire web,
    > > I couldn't find anything that does this =/ The code that I ended up using
    > > is as follows:
    > >
    > > string boolString(Object* obj_, size_t maxOutput_){
    > > string string_;
    > >
    > > for (size_t i=0; i<maxOutput_; i++){
    > >
    > > if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

    >
    > What is the above line supposed to do? What does obj_->boolean(i) return?
    > Note that sizeof is always computed at compile time.
    >
    > > if (obj_->boolean(i) == true)
    > > string_ += "1";
    > > else
    > > string_ += "0";
    > > }
    > > }
    > >
    > > return string_;
    > > }
    > >
    > > Is there any way to simplify this code?

    >
    > string boolString(Object* obj_, size_t maxOutput_){
    > stringstream stream_;
    >
    > for (size_t i=0; i<maxOutput_; i++)
    > // depending on the return type of boolean(), you can remove the cast
    > stream_ << bool(obj_->boolean(i));
    >
    > return stream_.str();
    > }


    Sometimes boolean() may return an integer, so doing a cast to a boolean
    is not a good idea. This is the reason behind the check of the size of
    whatever type boolean() returns. I thought that the sream can
    automatically convert to 1 or 0 from a boolean as

    stream_ += boolean(i);

    but this is not the case. However, if I repeat the previous with an
    integer, it works fine.
     
    aaragon, Nov 15, 2006
    #5
  6. Rolf Magnus wrote:
    > aaragon wrote:
    >
    >> Hi All,
    >>
    >> I'm trying to print boolean values and after I searched the entire
    >> web,
    >> I couldn't find anything that does this =/ The code that I ended up
    >> using is as follows:
    >>
    >> string boolString(Object* obj_, size_t maxOutput_){
    >> string string_;
    >>
    >> for (size_t i=0; i<maxOutput_; i++){
    >>
    >> if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

    >
    > What is the above line supposed to do? What does obj_->boolean(i)
    > return? Note that sizeof is always computed at compile time.
    >
    >> if (obj_->boolean(i) == true)
    >> string_ += "1";
    >> else
    >> string_ += "0";
    >> }
    >> }
    >>
    >> return string_;
    >> }
    >>
    >> Is there any way to simplify this code?

    >
    > string boolString(Object* obj_, size_t maxOutput_){
    > stringstream stream_;
    >
    > for (size_t i=0; i<maxOutput_; i++)
    > // depending on the return type of boolean(), you can remove the
    > cast stream_ << bool(obj_->boolean(i));
    >
    > return stream_.str();
    > }


    Consider that some locales do not have '1' or '0' as the default
    for outputting a bool value. If the OP wanted '1' or '0', it should
    probably be explicit.

    And how is using a stringstream simpler (better) than, say,

    for (size_t i=0; i<maxOutput_; i++)
    string_ += (bool(obj_->boolean(i)) ? '1' : '0');

    return string_;

    ?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #6
  7. aaragon

    Howard Guest

    "aaragon" <> wrote in message
    news:...
    >
    > Victor Bazarov wrote:
    >> aaragon wrote:
    >> > I'm trying to print boolean values and after I searched the entire
    >> > web, I couldn't find anything that does this =/ The code that I
    >> > ended up using is as follows:
    >> >
    >> > string boolString(Object* obj_, size_t maxOutput_){
    >> > string string_;
    >> >
    >> > for (size_t i=0; i<maxOutput_; i++){
    >> >
    >> > if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

    >>
    >> Why do you need to check the size? What does it give you?

    >
    > I need to check the size because the function may not return a bool but
    > an integer for example.
    >


    And how do you know that a value is a bool just because it's the same size
    as one?

    Also, boolean() is your own function, so you should already know what its
    return type is. It can't sometimes return a bool and other times return an
    int. (Unless, I guess, if "boolean" isn't really a function member of obj_
    at all, but is instead a function pointer which you assign elsewhere.)

    -Howard
     
    Howard, Nov 15, 2006
    #7
  8. aaragon wrote:
    > Victor Bazarov wrote:
    >> aaragon wrote:
    >>> I'm trying to print boolean values and after I searched the entire
    >>> web, I couldn't find anything that does this =/ The code that I
    >>> ended up using is as follows:
    >>>
    >>> string boolString(Object* obj_, size_t maxOutput_){
    >>> string string_;
    >>>
    >>> for (size_t i=0; i<maxOutput_; i++){
    >>>
    >>> if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

    >>
    >> Why do you need to check the size? What does it give you?

    >
    > I need to check the size because the function may not return a bool
    > but an integer for example.


    So? An integer is convertible to bool. Besides, sizes of 'bool' and
    'int' _can_ be the same. The check is no good. You need to check
    'typeid' to be sure.

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #8
  9. aaragon

    aaragon Guest

    >
    > Consider that some locales do not have '1' or '0' as the default
    > for outputting a bool value. If the OP wanted '1' or '0', it should
    > probably be explicit.
    >
    > And how is using a stringstream simpler (better) than, say,
    >
    > for (size_t i=0; i<maxOutput_; i++)
    > string_ += (bool(obj_->boolean(i)) ? '1' : '0');
    >
    > return string_;
    >
    > ?
    >


    Well, this definitely will work for booleans. But, the object I'm
    working on, would also be a container for other values (say double,
    integers) in which case boolean representation is no longer useful
    (forget about the name boolean(i), it could be anything, like
    value(i)). Therefore, the cast that you're using is not useful
    anymore.

    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
     
    aaragon, Nov 15, 2006
    #9
  10. Howard wrote:
    > "aaragon" <> wrote in message
    > news:...
    >>
    >> Victor Bazarov wrote:
    >>> aaragon wrote:
    >>>> I'm trying to print boolean values and after I searched the entire
    >>>> web, I couldn't find anything that does this =/ The code that I
    >>>> ended up using is as follows:
    >>>>
    >>>> string boolString(Object* obj_, size_t maxOutput_){
    >>>> string string_;
    >>>>
    >>>> for (size_t i=0; i<maxOutput_; i++){
    >>>>
    >>>> if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
    >>>
    >>> Why do you need to check the size? What does it give you?

    >>
    >> I need to check the size because the function may not return a bool
    >> but an integer for example.
    >>

    >
    > And how do you know that a value is a bool just because it's the same
    > size as one?
    >
    > Also, boolean() is your own function, so you should already know what
    > its return type is. It can't sometimes return a bool and other times
    > return an int. (Unless, I guess, if "boolean" isn't really a
    > function member of obj_ at all, but is instead a function pointer
    > which you assign elsewhere.)


    If it's a function pointer, the return value type is part of the type
    of that pointer. C++ is strict in that regard. You cannot have return
    value types change arbitrarily during run-time.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #10
  11. aaragon wrote:
    >> Consider that some locales do not have '1' or '0' as the default
    >> for outputting a bool value. If the OP wanted '1' or '0', it should
    >> probably be explicit.
    >>
    >> And how is using a stringstream simpler (better) than, say,
    >>
    >> for (size_t i=0; i<maxOutput_; i++)
    >> string_ += (bool(obj_->boolean(i)) ? '1' : '0');
    >>
    >> return string_;
    >>
    >> ?
    >>

    >
    > Well, this definitely will work for booleans. But, the object I'm
    > working on, would also be a container for other values (say double,
    > integers) in which case boolean representation is no longer useful
    > (forget about the name boolean(i), it could be anything, like
    > value(i)). Therefore, the cast that you're using is not useful
    > anymore.


    If for some twisted reason the 'boolean' function returns an 'int'
    and not a 'bool', it shouldn't be used here, which means you need
    to employ some kind of templated mechanism for rejecting classes
    with 'boolean' members returning anything except 'bool'. See your
    favourite C++ book on how to specialise your function on that.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #11
  12. aaragon

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:ejfp4p$hhq$...

    >> Also, boolean() is your own function, so you should already know what
    >> its return type is. It can't sometimes return a bool and other times
    >> return an int. (Unless, I guess, if "boolean" isn't really a
    >> function member of obj_ at all, but is instead a function pointer
    >> which you assign elsewhere.)

    >
    > If it's a function pointer, the return value type is part of the type
    > of that pointer. C++ is strict in that regard. You cannot have return
    > value types change arbitrarily during run-time.
    >


    Oh, right, of course. Thanks.

    -Howard
     
    Howard, Nov 15, 2006
    #12
  13. aaragon

    Rolf Magnus Guest

    aaragon wrote:

    >> What is the above line supposed to do? What does obj_->boolean(i) return?
    >> Note that sizeof is always computed at compile time.
    >>
    >> > if (obj_->boolean(i) == true)
    >> > string_ += "1";
    >> > else
    >> > string_ += "0";
    >> > }
    >> > }
    >> >
    >> > return string_;
    >> > }
    >> >
    >> > Is there any way to simplify this code?

    >>
    >> string boolString(Object* obj_, size_t maxOutput_){
    >> stringstream stream_;
    >>
    >> for (size_t i=0; i<maxOutput_; i++)
    >> // depending on the return type of boolean(), you can remove the cast
    >> stream_ << bool(obj_->boolean(i));
    >>
    >> return stream_.str();
    >> }

    >
    > Sometimes boolean() may return an integer, so doing a cast to a boolean
    > is not a good idea.


    Could you elaborate on how you think you did that? It's not possible. A
    function's return type is fixed at compile time. Somewhere, you declared
    the function, and part of that declaration is the return type. So if
    boolean() returns a bool, it will always do that.

    > This is the reason behind the check of the size of whatever type boolean()
    > returns.


    The size of bool depends on the implementation. Most often, it's the same as
    char or the same as int.

    > I thought that the sream can automatically convert to 1 or 0 from a
    > boolean as
    >
    > stream_ += boolean(i);


    You need operator <<, not += to put something into a stream.
     
    Rolf Magnus, Nov 15, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    2
    Views:
    486
    Weng Tianxiang
    Jun 21, 2005
  2. Ignacio Machin

    Re: Convert DataRow content to bool

    Ignacio Machin, Jul 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    Ignacio Machin
    Jul 7, 2003
  3. Eliyahu Goldin

    Re: Convert DataRow content to bool

    Eliyahu Goldin, Jul 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    367
    Eliyahu Goldin
    Jul 7, 2003
  4. RowDataBound bool to string

    , May 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    183
    yoga weazel
    May 5, 2006
  5. Sandman
    Replies:
    7
    Views:
    221
    Anno Siegel
    Aug 3, 2004
Loading...

Share This Page