Greetings! So my question is: Is it possible to create your own data type? Not a class, or a structure, that builds from predefined C++ types, but your very own. For example a 128bit integer type, or a very very long floating point data type, or a binary number type to work with bit patterns. Something like these.

Also, I would like to know what is the smallest size you can address in C++, is it 1 byte or can you go further? Thank you very much.

You just described classes and operator overloading.

class myLongInt;

Then later
myLongInt a, b, c;

With suitable initialisation, and an appropriate method or two, you can do
c = a + b;

Thanks, but just to recap, my question is: Is it possible to create a data type which is not bounded to predefined C++ data types. I can create my own classes. but that would still rely on them. ( int, char, double whatever.. ). E.g.: what If I want to create a 6000 byte long number? ( I know, it would be insane, but just for the sake of curiosity.)

If you don't intend to use any of the predefined C++ data types in your implementation, what do you propose to use?

A 6000 byte long number could be implemented, I would probably use an array with 6000 elements, but that still uses base C++ data types.

(Ignoring the fact that arbitrary precision math class is probably already available.)

I'd recommend getting over the "I don't want to use the ___, how do I do ___" mentality and focus on the objective of what you want to be done. Use the tools that are available.

If you don't have an objective that has been assigned, from school or work, then develop your own objective and assign it to yourself. This question appears to be argumentative on the surface. If you have a real-world situation that would make this line of inquiry make sense, feel free to present it.

commented: Thank you for your answer it reflects you also like to think out of the box. :) +1
commented: Good +29

Thanks, but just to recap, my question is: Is it possible to create a data type which is not bounded to predefined C++ data types. I can create my own classes. but that would still rely on them. ( int, char, double whatever.. ). E.g.: what If I want to create a 6000 byte long number? ( I know, it would be insane, but just for the sake of curiosity.)

What the fuck is wrong with you? The mechanism for creating datatypes in C++ is classes. Why wouldn't you use classes?

In the future, please keep your insanity to yourself. The insanity isn't wanting a 6000 byte number, it's asking how to create datatypes when the mechanism for creating datatypes is staring you in the face.

I'm being mean. If you wanted to create a 6000 byte long number, you would do something like this:

class int6000 {
    uint32_t data[1500];
  public:
    // ...
};

uint32_t is not a C++-standard type, I don't think, so substitute whatever 4-byte integer type you want if it doesn't work on your compiler (try #include <limits.h> to make uint32_t available).

>Is it possible to create your own data type? Not a class, or a
>structure, that builds from predefined C++ types, but your very own.
What do you think a class is supposed to be? It's a user-defined type. What you seem to want is magic: a magic type that magically does something useful without any underlying logic. It doesn't work that way. There's no magic in programming. There's always a base that you build on top of to create something new. In the case of C++, you create new types by leveraging the power of existing types. I find your question to be misguided and nonsensical.

>Also, I would like to know what is the smallest size you can address in C++
char is the smallest addressable unit in C++.

Well guys I didn't want to rock the boat - I understand all of your points - that is be within C++ boundaries. I got that.

during my adventures in C++ i also wanted to create a new data independent from the types defined in c++;not as an objective, but out of mere curiosity...
If you really want to do that i would recommend using the __asm__ keyword
within assembly data is not predefined into types; all data is just sequences of bytes stored in the data segment of a program. basic syntax for creating such a thing would be

__asm__ resb 64 myCrazyData;
     __asm__ resb 64 myCrazyData;

or alternately

__asm__
      {
            resb 64 sixty_four_byte_var
      };

I'm not sure about the exact syntax but these will give you the basic idea.
Also, you will most likely have to use assembler math commands(inc,add,sub,mul,div,etc.) i would recommend reading a text on the different assembler math commands as there are a wide variety. Also, when in assembler variable scope does not apply and you can multiply and divide and variable you have declared in c in your assembler except those that have already been garbage collected. here is an example:

int main()
      {
           int main_asm_var=2;
           asm_func();
      }
      int asm_func()
      {
           int local_asm_var=1;
           __asm__
           {
                resb 4 four_byte_var
                mov eax four_byte _var
                add local_asm_var eax
                add main_asm_var eax
           };
      }

*note* some of the assembler syntax may be incorrect as i have not worked in assembler for some years
*another note*after you create data chunks larger than 4bytes math can get very complicated because of CPU register size- i would suggest reading up on it before you try it.
both add methods work because there is no variable scope. Also, this is just one way i have seen 1 (windows) compiler use the asm keyword- the GNU C Compiler(GCC), for example, takes each command as a string in a function-like interface

asm("command 1","command 2",etc.);

as for smallest data types accessible-usually people stick with the byte or char data types-for the unicode seet of chars you can use wide_t(2 bytes) and if you really want to manage individual bits, theres always bit fields. Bit fields are declared in structs(a collection of data) or unions(more than one type of data which shares the same memory location) in this form

struct tcp
      {
           unsigned short int th_sport;
           unsigned short int th_dport;
           unsigned int th_seq;
           unsigned int th_ack;
           unsigned char th_x2:4, th_off:4;

           //here is the union which combines the 1byte of flags(char) with the
           //6 1-bit bitfields that represent the tcp flags
           union FLAGS
           {
                unsigned char th_flags;
                unsigned char urg:1,ack:1,psh:1,rst:1,syn:1,fin:1;
           }flags;

           unsigned short int th_win;
           unsigned short int th_sum;
           unsigned short int th_urp;
      };

here i am using an actual example from one of my programs to show the use of bitfields-each bit-field represents a 1-byte flag used to indicate something in a TCP packet header-here is a bitfield on its own:

char myBitField:1

char myBitField:1
However, before you go ahead and start bit-fielding willy-nilly, I would recommend reading more on them as they have some serious safety drawbacks due to the inability of most machines to manipulate individual bits and differing machine bit-order;

P.S. I should probably also note that technically this is not c++ and I wholly agree with the above posters that you should try to build on the foundations already laid in c++...they are absolutely correct in that you should use what already exists(classes, arrays, etc.) as opposed to trying to actually create a data type with assembler. What I am showing you is not really c++ it is using assembler through a window in c++

commented: nice post, even though you are a year late with your answer. +25
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.