Is it possible to create your very own data type?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

Is it possible to create your very own data type?

 
0
  #1
Feb 21st, 2009
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.
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #2
Feb 21st, 2009
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;
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

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

 
0
  #3
Feb 21st, 2009
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.)
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

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

 
2
  #4
Feb 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

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

 
0
  #5
Feb 21st, 2009
Originally Posted by BevoX View Post
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #6
Feb 21st, 2009
>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++.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

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

 
0
  #7
Feb 21st, 2009
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.
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC