silly question

?

=?iso-8859-1?Q?Juli=E1n?= Albo

Thomas Matthews escribió:
One item that others haven't mentioned is that you
declare the array as type "char" while you assign it
"int" values. Although many compilers may not issue a warning,
I believe it should, and you should turn up the warning levels.

Perhaps you must switch to Pascal? ;)

Regards.
 
K

Karl Heinz Buchegger

Thomas said:
class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)
table[index] = index;

Here you assign an "int" value (index)
into a "char" type (table[]).

In many systems, a char type is smaller than an
int type. In these systems, an "invisible" truncation
or conversion will take place. Ugly, very ugly.

truncation: yes
conversion: no

after all a char is nothing more then a small int.
The only special thing about a char is the way it is handled
during input/output. In all other aspects one can treat
a char as beeing a variation of int with a lower bit count
(and you do not know if its signed or unsigned).
Just like a long is a variation of int with a higher bit count.
Try this:
for (char index = 0; index < 64; ++index)
table[index] = index;

Although the number of bits in a char is not defined there
is a lower limit. A char must have at least 8 bits.
Clearly more then enough to store the number 64 in it.
 
J

Jerry Coffin

[ ... ]
In addition to the other comments, you have a private constructor. How will
you ever create an object of this class?

Private ctors are really fairly common -- singletons (for one example)
nearly never have any ctors that aren't private (though they normally
also befriend a static function that uses a private ctor).
 
J

Jerry Coffin

[email protected] says... said:
truncation: yes
conversion: no

Truncation: maybe
Conversion: Definitely YES

This is all covered in $4.7 "Integral conversions", which is part of
section 4, "Standard conversions".
after all a char is nothing more then a small int.
The only special thing about a char is the way it is handled
during input/output. In all other aspects one can treat
a char as beeing a variation of int with a lower bit count
(and you do not know if its signed or unsigned).
Just like a long is a variation of int with a higher bit count.

According to the standard, there are conversions and promotions.
Anytime something that's one integer type gets assigned to a variable of
another integer type, it has to go through a promotion or a conversion.
In many cases, the value of the result will be the same, but that
doesn't prevent it from being a conversion. Just for example:

int x = 0L;

This involves a conversion, even though it's guaranteed that value will
remain unaffected by the conversion.
 
K

Karl Heinz Buchegger

Jerry said:
Truncation: maybe
Conversion: Definitely YES

This is all covered in $4.7 "Integral conversions", which is part of
section 4, "Standard conversions".

I should have checked the standard how the word 'conversion' is
used. Thanks for correcting.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top