dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Can I make my own datatype in C++?

  1. #1
    Join Date
    Nov 2018
    Posts
    19

    Can I make my own datatype in C++?

    I know that classes can be cleverly used to make custom datatype-like classes. But you have to rely on existing datatypes like the char datatype.

    But can I make, for instance, my own datatype that is a 32-byte integer type (for example)?

    If you were to make an arbitrary size datatype then using char datatypes has to be really inefficient right? Then how do people implement arbitrary size datatypes?

    Would the 32-byte integer type be greater in efficiency than a class maintaining 32 chars?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,544

    Re: Can I make my own datatype in C++?

    The short answer is yes. A char datatype is just an interpretation of 8 bits of memory. A 32-byte integer type would be an interpretation of 256 bits of memory.

    Re arbitrary size datatype. If the size is known at compile time then there are algorithms that enable add, sub, multiply, divide etc (usually in asembler). if the size is only known during run-time, then this is still possible but becomes much more complex. This is usually used for what is called bitint (Arbitrary precision arithmetic). See https://en.wikipedia.org/wiki/Arbitr...ion_arithmetic
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  3. #3
    Join Date
    Nov 2018
    Posts
    19

    Re: Can I make my own datatype in C++?

    How do bitint implement? Using char datatype and classes? Would there theoretically be a better way that could have been supported by the langauge (that allows arbitrary precision)?

    What if the size if known at compile time what can I use instead of bitint? Is it much faster than bitint?
    Last edited by Numb; January 10th, 2019 at 06:41 AM.

  4. #4
    Join Date
    Feb 2017
    Posts
    389

    Re: Can I make my own datatype in C++?

    Quote Originally Posted by Numb View Post
    Would there theoretically be a better way that could have been supported by the langauge?
    In principle a program cannot be faster than the hardware it runs on. Therefore C++ defines a set of fundamental types that almost certainly will have hardware support on every computer,

    https://en.cppreference.com/w/cpp/language/types

    Not everything in programming is about raw number crunching (it's about abstraction too) but in numerical scientific applications for example there is a price to pay for replacing the fundamental types. Before you do that it's better to first try to reformulate and/or rescale the problem so it won't be necessary. Do numbers really have to be that big (or small)? Ar all those decimal places really needed?
    Last edited by wolle; January 11th, 2019 at 01:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)