wahaha wrote:
> On Feb 22, 6:22 pm, diligent.sn...@gmail.com wrote:
>> On Feb 23, 2:58 am, "webinfin...@gmail.com" <webinfin...@gmail.com>
>> wrote:
>>
>>
>>
>>> #include <iostream>
>>> #include <string>
>>
>>> using namespace std;
>>
>>> int main() {
>>> char t1;
>>> struct t2{
>>> char x;
>>> };
>>
>>> cout << sizeof(t1) << " " << sizeof(t2) << endl;
>>
>>> typedef char& x1;
>>> struct x2{
>>> x1 x;
>>> };
>>
>>> cout << sizeof(x1) << " " << sizeof(x2) << endl;
>>
>>> }
>>
>>> output:
>>> 1 1
>>> 1 4
>>
>>> Can anybody tell me why the size of x2 is 4? Why it's not 1?
>>
>> Hello,
>>
>> This question was answered
>> here:http://groups.google.com/group/alt.c...+/browse_threa...
>> (in particular Karl Heinz Buchegger's answer)
>>
>> It all comes down to the fact that the implementation of a
>> reference
>> is compiler dependent, and a compiler may use a pointer to
>> implement
>> reference semantics (which probably explains size of 4 bytes).
>>
>> Regards.
>
> Thank you. But how it will explain sizeof(char&) = 1 but
> sizeof(struct x2) = 4?
When applied to a reference, the sizeof operator gives the size of the
item referred to. In this case a char.
When applied to a struct, the sizeof operator gives the size of the
struct.
Bo Persson