0

I'd like to know how to create my own byte level datatype (like int, short, long, etc.). I know some will ask "Why!!? this is crazy!!" let's say I don't wanna use classes or structs. And I wanna know how to create that kind of datatype for C/C++ even if I have to use asm code for that (which I guess I'll have to use). Or is there a book that could help?

Can anyone please help? thank you.

Paiku Han
  • 581
  • 2
  • 16
  • 38
  • 1
    The only way to 'create' these data types is to re-write/write a c++/c compiler in a way that parses and stores your new types using assembly instructions. I would say get a good book on compilers and grab the source for GCC. – Jimmy Johnson Oct 07 '13 at 22:14
  • 1
    What you're asking for is not possible. Please explain better what you're trying to achieve and somebody may be able to help you... – R.. GitHub STOP HELPING ICE Oct 07 '13 at 22:14
  • 2
    Sounds like a typical [X-Y Problem](http://www.perlmonks.org/index.pl?node_id=542341) to me. – Jongware Oct 07 '13 at 22:17

1 Answers1

1

If you exclude all the methods C and C++ give you to create new types then you can't create new types.

It's simple with a class:

Make a class that has some sort of storage. If you insist on doing everything manually then just make this an array of bytes. Overload all the operators you want to support.

class my_datatype
{
public:
   my_datatype();
   // overload operators
private:
   uint8_t[sizeof_my_datatype] data;
};

Other than classes and structs, the only way to "make" a new datatype in C and C++ is a typedef. That's nothing more than an alias, though.

Adam
  • 16,808
  • 7
  • 52
  • 98
  • OP does not want to use classes or structs, but OP doesn't really seem to know what s/he wants... – R.. GitHub STOP HELPING ICE Oct 07 '13 at 22:14
  • the problem here is I don't think this would work for C only compiler – Paiku Han Oct 07 '13 at 22:14
  • You said c++ as well. – Adam Oct 07 '13 at 22:15
  • yes I said C++ as well but I meant if it works on C it should work on C++ just like int, double,etc. which is not the case for class – Paiku Han Oct 07 '13 at 22:17
  • If you exclude all the methods C and C++ give you to create new types then you can't create new types. – Adam Oct 07 '13 at 22:18
  • @R.. whell i do know wtha i want ;) .. I'd like to create my own datatype which consist of a two 8-bits bytes in which I can store positive number only from 0 to (2^16)-1 even though what I actually want to store in it are character – Paiku Han Oct 07 '13 at 22:21
  • @PaikuHan: Why don't you want to use `uint16_t`? – R.. GitHub STOP HELPING ICE Oct 07 '13 at 22:23
  • I.e. you want to recreate `uint16_t`. – Adam Oct 07 '13 at 22:23
  • @Adam there still is asm left to write in a c source. I guess this is how int, char, long and all the other were created?? – Paiku Han Oct 07 '13 at 22:23
  • You can write assembly all you want but that won't create a datatype that is exposed to other C code. You have to put assembly inside a function which will look like any other C function. – Adam Oct 07 '13 at 22:25
  • 2
    No, it's not. `int`, `char`, and `long` are part of the language. They are neither implemented in C nor in asm, but as part of the compiler's task of translating the C program to whatever final form it will be run in. – R.. GitHub STOP HELPING ICE Oct 07 '13 at 22:25
  • Oh! ok then I guess that's it I have to create a uint16_t – Paiku Han Oct 07 '13 at 22:26
  • 1
    You describe a "wide character". I.e. a unicode character. Believe me, you're far from the first person do do this. – Adam Oct 07 '13 at 22:27
  • @Adam: Huh? I see no indication that this problem has anything to do with Unicode or storing characters; if it did, a 16-bit type would be inappropriate, as Unicode is 21-bit. – R.. GitHub STOP HELPING ICE Oct 07 '13 at 22:29
  • He says "what I actually want to store in it are character". So either it's UTF-16 or a whole 'nother reinvention of the wheel. – Adam Oct 07 '13 at 22:32
  • @Adam you got it right. that's what I exactly want but the problem here is that C does not support operator overload. – Paiku Han Oct 07 '13 at 22:32
  • 1
    Just use wide characters as supplied by your platform. See here for an overview: http://stackoverflow.com/a/11287282/321772 – Adam Oct 07 '13 at 22:34
  • @R.. 16-bit is the definition given by wikipedia maybe I misunderstood – Paiku Han Oct 07 '13 at 22:34
  • I find that in the western world UTF-8 is the most common, because it only uses extra bytes for the few characters not in ASCII. The UTF-16 and 32 are mainly going to be used in countries where every character is wide (eg. Chinese), or in places where you need a fixed length per character. – Adam Oct 07 '13 at 22:37