What I meant is: in case1 parsing of :
*cout << test ? "A String" : 0 << endl;    *
is messed up because operator '*<<*' has higher precedence than ternary (*?:
*) operator.

Firstly, *cout << test* will be parsed then, an attempt for *0 <<
endl*would be made, which will fail because, arguments are not
compatible! it
makes no sense to left-shift 0 by 'endl'.
If you change *endl* to *5*  that is,

* cout << test ? "A String" : 0 << 5;    *

expression will compile and work but its' something not what you want.
ternary operator is totally wasted here.

*cout << test ? "A String" : 0 << 5;*
How this expression will work ? It will first execute *cout <<
test*(outputting 0) return value is a
*reference to cout object *itself.
secondly, left shift of 0 by 5, which will evaluate to 0 again.
Now, you have *cout(ref) ? "A String" : 0* which will evaluate to "A
String" since reference of cout will be *not NULL*. but this value "A
String" will be wasted as you are not doing anyting with it.

>>>>and why 0 is converted to base..i know exp3 is convertered to exp
to..so int to array..???base array comes in int???and what is 0??

In expression: *expr1 ? expr2 : expr3*

as you already know for the above expression: return type is determined by
type of *expr2* and if *expr3* is not the same type as *expr2* then
compiler will 'try' to convert it to type of *expr2*.
say, expr2 is float and expr3 is int. then expr3 will be converted to float.
if expr2 is int and expr3 is float, then expr3 will be converted to int.
if expr2 is char* and expr3 is float, error will be thrown because there is
no way compiler can interpret a float as an address.
if expr2 is char* and expr3 is int, again it will be an error except for 0.
as 0 stands for NULL pointer.

so, in-  *cout << test ? "A String" : 0*
0 is a special case pointing to NULL, that's why it works.

>>>>what if we have 1 in case of 0?
you'll again get an error because you cannot assign an int value of 1 to a
char* like that. (the exact procedure to assign is to cast it to (void*)
and so on... but you are skipping all those steps. hence, an error) or if
you use something like:
cout << test ? "A String" : (char*)1;
or, cout << test ? "A String" : (char*)0x1523; then it should work.

But generally you should avoid falling into these intricate details.
Points to note:
1. Always use braces() when in doubt.
2. Note how the return value type for ternary operator is determined.

On 2 November 2012 00:40, rahul sharma <rahul23111...@gmail.com> wrote:

>
> I cant get to you..please explain...what will first statement expanded to
> during compilation..??.and why 0 is converted to base..i know exp3 is
> convertered to exp to..so int to array..???base array comes in int???and
> what is 0??what if we have 1 in case of 0?
>
> On Thu, Nov 1, 2012 at 5:20 PM, Saurabh Kumar <srbh.ku...@gmail.com>wrote:
>
>> There's nothing to do with the type of "A String".
>> The reason for which the first code gives compilation error is: operator
>> (<<) has higher precedence than ternary operator (?:) so, without braces
>> your are actually messing up the parsing of "cout << << << "stream.
>>
>> * cout << test ? "A String" : 0 << endl;    *
>>
>> consider removing the last "*<<endl*". It shall give no compilation
>> error.
>>
>> * cout << test ? "A String" : 0;* // this compiles and runs fine with my
>> compiler(g++ 4.5), but as *cout << test* will take precedence, there is
>> no point of putting a ternary operator there, unless you use braces, of
>> course.
>>
>>
>> In teh second case however, reason given is valid:
>>  i.e. return type of ternary operator is determined by "A String" and the
>> expression 0 will be taken as address of a string location. Hence, there is
>> no guarantee of output.
>>
>>
>> On 31 October 2012 19:52, rahul sharma <rahul23111...@gmail.com> wrote:
>>
>>> plz read carefully
>>>
>>>
>>> On Wed, Oct 31, 2012 at 10:18 AM, Tamanna Afroze <afroze...@gmail.com>wrote:
>>>
>>>> sorry for my last post, i didn't look carefully at the code. I think
>>>> without bracket the ternary expression is incomplete, that's why; first
>>>> code doesn't compile correctly.
>>>>
>>>>
>>>>
>>>> On Wed, Oct 31, 2012 at 9:51 AM, Tamanna Afroze <afroze...@gmail.com>wrote:
>>>>
>>>>> both codes are same. Where is the difference?
>>>>>
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To post to this group, send email to algogeeks@googlegroups.com.
>>>> To unsubscribe from this group, send email to
>>>> algogeeks+unsubscr...@googlegroups.com.
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to