By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

byte vs char

P: 2
Hi,

This is a question about declaring a signed 8bit numeric type in C++ that prints like a numeric variable and not like a char variable. I would like to declare a type with name 'byte' which represents 8bit numeric values. Of course C++ has char with the right size. So I tried
------
typedef char byte;
------
This is fine, until I tried
---------
byte b = 1;
cout << b;
---------
This (as expected) does not output '1' (as I would like), but rather the char with ascii code 1. Since I would prefer my numeric types to print as their decimal value (like int, doulbe, etc.), I tried
--------
typedef char byte;
std::ostream &operator<<(std::ostream &os, const byte &b) {
os << (int)b;
return os;
}
-------
Now
--------
char b=62;
cout << b;
-------
prints '62' and not 'A'. It seems that typedef is simply aliasing and not actually creating a new type. My next attempt is to create a new type which just wraps char
---------
class byte {
char v;
public:
byte() : v() { }
byte(const byte &w) : v(w.v) { }
byte(const char &w) : v(w) { }

byte &operator=(const byte &w) { v = w.v; return *this; }
byte &operator=(const char &w) { v = w; return *this; }

operator char() { return v; }

friend std::ostream &operator<<(std::ostream &, const byte &);
friend std::istream &operator>>(std::istream &, byte &);
};

std::ostream &operator<<(std::ostream &os, const byte &b) {
os << (int)b.v;
return os;
}
------------
This works fine, except when compiling with g++ (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
------
byte b = 0;
b++;
-----
produces:
error: no 'operator++(int)' declared for postfix '++', trying prefix operator instead
error: no match for 'operator++' in '++b'

This is rather odd because
------
byte b = 0;
b = b + 1;
-----
compiles fine (the conversion to unsigned char is called as intended).

So it seems like I have to declare ++, --, +=, .... At this point I gave up on this attempt because it seemed to me that the solution is already going out of hand for what I wanted originally.

Is there an elegant solution to this?

Thanks,
Vladimir
Nov 16 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 10K+
P: 11,448
You're on the right track: indeed the old C typedef just creates a new name for
an already existing type and the pre- or postfix ++ isn't mapped to a x+1 binary
operator. If takes a bit of work to create an entirely new type that accepts the
same operators as the built-in primitives.

You can define the operators 'op' in terms of the 'op=' operators. The first one just
calls the second one on a copy of the the left hand operand. So you have to
create a copy constructor (which is trivial in your case).

Compare your 'byte' class with Bjarne Stroustrup's 'complex' class; they're similar.

kind regards,

Jos
Nov 16 '08 #2

Banfa
Expert Mod 5K+
P: 8,916
Depending on how often you output bytes using cout you may find it easier just to static_cast your byte type'd variables to int in your cout statements.

This is one area where I have always felt that C++s attempt to guess how you want your variable output was rather less useful than C's basic insistence that you tell it how to do it.

Also if you want byte as an unsigned 8 bit variable then you need to define it as
typedef unsigned char byte
because the signedness of char is platform dependent.
Nov 16 '08 #3

P: 2
Thanks, guys!

I fiddled with this some more over the weekend and found out that the following code works for me. Thought I would post this in case somebody is fighting with the same...
---------------------
template <class T>
class byte_templ {
T v;

public:
byte_templ<T>() : v() { }
byte_templ<T>(const T &w) : v(w.v) { }
byte_templ<T>(const byte_templ<T> &w) : v(w) { }

byte_templ<T> &operator=(const byte_templ<T> &w) { v = w.v; return *this; }
byte_templ<T> &operator=(const T &w) { v = w; return *this; }

operator T() const { return v; }
operator T &() { return v; }
};

std::ostream &operator<<(std::ostream &os, const byte_templ<unsigned char> &b) {
os << (unsigned short)b;
return os;
}

std::istream &operator>>(std::istream &is, byte_templ<unsigned char> &b) {
unsigned short i;

is >> i;
b = (unsigned char)i;
return is;
}

typedef byte_templ<unsigned char> byte;
---------------------
Now
---------------------
cin >> b;
b++;
cout << b;
---------------------
works as intended. Basically, I needed to add the cast to (unsigned char &), because 'b++' implicitly contains an assignment...

Best, Vladimir
Nov 17 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.