adding a bool to a string 
November 15th, 2006, 05:15 PM
| | | adding a bool to a string
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? | 
November 15th, 2006, 05:35 PM
| | | Re: adding a bool to a string
aaragon wrote: Quote:
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. Quote:
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();
} | 
November 15th, 2006, 05:35 PM
| | | Re: adding a bool to a string
aaragon wrote: Quote:
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? Quote:
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. Quote:
}
}
>
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 | 
November 15th, 2006, 05:45 PM
| | | Re: adding a bool to a string
Victor Bazarov wrote: Quote:
aaragon wrote: Quote:
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. Quote:
> Quote:
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.
> Quote:
}
}
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
| | 
November 15th, 2006, 05:45 PM
| | | Re: adding a bool to a string
Rolf Magnus wrote: Quote:
aaragon wrote:
> Quote:
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.
> Quote:
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. | 
November 15th, 2006, 05:55 PM
| | | Re: adding a bool to a string
Rolf Magnus wrote: Quote:
aaragon wrote:
> Quote:
>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.
> Quote:
> 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 | 
November 15th, 2006, 05:55 PM
| | | Re: adding a bool to a string
"aaragon" <alejandro.aragon@gmail.comwrote in message
news:1163617470.334923.67870@k70g2000cwa.googlegro ups.com... Quote:
>
Victor Bazarov wrote: Quote:
>aaragon wrote: Quote:
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 | 
November 15th, 2006, 05:55 PM
| | | Re: adding a bool to a string
aaragon wrote: Quote:
Victor Bazarov wrote: Quote:
>aaragon wrote: Quote:
>>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 | 
November 15th, 2006, 05:55 PM
| | | Re: adding a bool to a string
> Quote:
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. Quote:
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
| | 
November 15th, 2006, 05:55 PM
| | | Re: adding a bool to a string
Howard wrote: Quote:
"aaragon" <alejandro.aragon@gmail.comwrote in message
news:1163617470.334923.67870@k70g2000cwa.googlegro ups.com... Quote:
>>
>Victor Bazarov wrote: Quote:
>>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 | 
November 15th, 2006, 06:05 PM
| | | Re: adding a bool to a string
aaragon wrote: Quote: Quote:
>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 | 
November 15th, 2006, 06:05 PM
| | | Re: adding a bool to a string
"Victor Bazarov" <v.Abazarov@comAcast.netwrote in message
news:ejfp4p$hhq$1@news.datemas.de... Quote: Quote:
>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 | 
November 15th, 2006, 09:15 PM
| | | Re: adding a bool to a string
aaragon wrote: Quote: Quote:
>What is the above line supposed to do? What does obj_->boolean(i) return?
>Note that sizeof is always computed at compile time.
>> Quote:
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. Quote:
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. Quote:
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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|