943,743 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2677
  • C++ RSS
Feb 21st, 2009
0

Is it possible to create your very own data type?

Expand Post »
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.
Similar Threads
Reputation Points: 77
Solved Threads: 12
Junior Poster in Training
BevoX is offline Offline
57 posts
since Jan 2009
Feb 21st, 2009
0

Re: Is it possible to create your very own data type?

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;
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 21st, 2009
0

Re: Is it possible to create your very own data type?

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.)
Reputation Points: 77
Solved Threads: 12
Junior Poster in Training
BevoX is offline Offline
57 posts
since Jan 2009
Feb 21st, 2009
2

Re: Is it possible to create your very own data type?

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Feb 21st, 2009
2

Re: Is it possible to create your very own data type?

Click to Expand / Collapse  Quote originally posted by BevoX ...
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 **** 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:

C++ Syntax (Toggle Plain Text)
  1. class int6000 {
  2. uint32_t data[1500];
  3. public:
  4. // ...
  5. };

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).
Last edited by Rashakil Fol; Feb 21st, 2009 at 1:56 pm.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Feb 21st, 2009
1

Re: Is it possible to create your very own data type?

>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++.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 21st, 2009
0

Re: Is it possible to create your very own data type?

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.
Reputation Points: 77
Solved Threads: 12
Junior Poster in Training
BevoX is offline Offline
57 posts
since Jan 2009
Jan 24th, 2010
0
Re: Is it possible to create your very own data type?
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
C++ Syntax (Toggle Plain Text)
  1. __asm__ resb 64 myCrazyData;
  2. __asm__ resb 64 myCrazyData;
or alternately
C++ Syntax (Toggle Plain Text)
  1. __asm__
  2. {
  3. resb 64 sixty_four_byte_var
  4. };
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:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int main_asm_var=2;
  4. asm_func();
  5. }
  6. int asm_func()
  7. {
  8. int local_asm_var=1;
  9. __asm__
  10. {
  11. resb 4 four_byte_var
  12. mov eax four_byte _var
  13. add local_asm_var eax
  14. add main_asm_var eax
  15. };
  16. }
*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
C++ Syntax (Toggle Plain Text)
  1. 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
C++ Syntax (Toggle Plain Text)
  1. struct tcp
  2. {
  3. unsigned short int th_sport;
  4. unsigned short int th_dport;
  5. unsigned int th_seq;
  6. unsigned int th_ack;
  7. unsigned char th_x2:4, th_off:4;
  8.  
  9. //here is the union which combines the 1byte of flags(char) with the
  10. //6 1-bit bitfields that represent the tcp flags
  11. union FLAGS
  12. {
  13. unsigned char th_flags;
  14. unsigned char urg:1,ack:1,psh:1,rst:1,syn:1,fin:1;
  15. }flags;
  16.  
  17. unsigned short int th_win;
  18. unsigned short int th_sum;
  19. unsigned short int th_urp;
  20. };
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:
C++ Syntax (Toggle Plain Text)
  1. 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++
Last edited by maf5693; Jan 24th, 2010 at 1:00 pm.
Reputation Points: 35
Solved Threads: 0
Newbie Poster
maf5693 is offline Offline
15 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ help regarding leap year
Next Thread in C++ Forum Timeline: How do you read different data types?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC