Skip to content
← Back to Community
I Made my Own Data Type (C++)!
Profile icon
h
has Hacker Plan
DynamicSquid

It's called "dsq::squid". The namespace "dsq" stands for "dynamic squid". If you want to see how I created this, take a look at the attached repl. Also, if you want to learn how to make your own data type, check out this [tutorial] (https://repl.it/talk/learn/Overloading-Operators-in-C/35455). Otherwise, let's see what "squid" can do!


So my data type can actually accommodate most primitive types, making it dynamic. It can hold an integer, string, float, and a few more! Here's the basic syntax:

dsq::squid<data_type> variable_name = value; dsq::squid<int> integerVariable = 10; dsq::squid<char> characterVariable = 's';

Here's the list of types it can hold:

  • bool
  • char
  • short, int, long
  • float, double
  • std::string

And the cool thing is, each type has it's own special method! Now let's take a look at bool.


Boolean Squids

// we'll be using the namespace for now using namespace dsq; squid<bool> boolVar = true; // you can even assign it to another boolean! bool anotherBoolVar = boolVar; // also works in if statements! if (boolVar) std::cout << "boolVar is " << boolVar; // and you can use it with `std::cout`! // `.size()` returns the size in bytes of the variable size_t sizeOfBool = boolVar.size();

As you can see, with the power of overloaded operators, we can make the object boolVar function like a traditional boolean!

Well, that's just bool. Next, characters!


Character Squid

squid<char> charVar = 's'; // just like bool, 'charVar' behaves like a traditional character char anotherCharVar = charVar; // you can even increment it! (the ascii value) ++charVar; // prefix and charVar++; // postfix! // gets the ascii value! int asciiValue = charVar.ascii(); // just like bool, you can get the size of the variable size_t sizeOfChar = charVar.size();

What's special about char is that you can actually get the ascii value of it! Should come in handy sometimes.

Well, next up, integers!


Integer Squids

// works with short and long as well squid<int> intVar = 10; // just like the rest, 'intVar' functions like a traditional int variable int anotherIntVar = intVar; // also supports increments ++intVar; intVar++; // and more math operations intVar += 10; intVar %= 3; // and even additon! anotherIntVar = intVar + 10; // and even rounding! intVar = intVar.round(); // you can even convert it to a string! std::string = intVar.to_str();

What makes the int type so special is the round method!

intVar.round(place, method);

The place is based 10. So you can round it by the 10th place, 100th place, or even 100000th place. The default is 10.

The method is how you would like to round it. Up, down, or normally.

Up - 3.14 becomes 3.2;
Down - 3.14 becomes 3.1
Normal - 3.14 becomes 3.1

There's also a to_str() method which converts the int to a string.

Next up, decimals!


Decimal Squids

So for decimals, it's actually very similar to ints, but the round function works a little differently. You specify how many decimals places you would like to remove (default is 3).

squid<double> doubleVar = 3.1415; doubleVar = doubleVar.round(); // default is 3 doubleVar = doubleVar.round(1); // one decimal place

Another special feature is the decimal_places() method, which counts the number of decimal places. It returns and int.

doubleVar.decimal_places();

And lastly, strings!


String Squids

squid<std::string> strVar = "dynamic "; // concatenate strings strVar += "squid"; // multiple strings! strVar *= 2; // dynamic squiddynamic squid // access a character from the string! char letter = strVar[2]; // get the length int strLen = strVar.length() // and erase it! works just like `.erase()` strVar.erase(1, 5);

So a few things here. Subscript operator, multiplication, length, and erase.


And yeah! That's all the data types of the dsq::squid data type. Comment below if you want me to add more. Also, one more thing:

// you can count the number of squids!!! int numberOfSquidVariables = countSquids;

Don't forget to upvote!

Voters
Profile icon
programmeruser
Profile icon
CodingGoose
Profile icon
mkw
Profile icon
firefish
Profile icon
Yeidenx112
Profile icon
Codemonkey51
Profile icon
ChezCoder
Profile icon
Jakman
Profile icon
CodeLongAndPros
Profile icon
DynamicSquid
Comments
hotnewtop
Profile icon
Codemonkey51

WAIT:

attacked repl

YOU ATTACKED A REPL HOW DARE YOU MONSTER

Profile icon
DynamicSquid

@Codemonkey51 oh oops, lol. I meant "attached" xD

Profile icon
Codemonkey51
Profile icon
xxpertHacker

I don't know, I see C casts in there (int), and I can't do ++ on the bool.
Some of this could've had better performance, for example, constexpr for .size.

constexpr int size = sizeof variable;

Also, why was it a function?

Really, most of this uses the C++ stdlib, makes it private, and exposes even less than std:: does.

Thinking about this... I might be able to make something relatively useful.

Now really, I was preparing to make a type-safe output system relatively similar to (w)cout and I had seen squid& as the return value of the operator, which is a reference to an object of the type squid, yet attempts to do so in my own code fail.

Profile icon
DynamicSquid

@StudentFires a type safe output system would be kind cool

Profile icon
xxpertHacker

@DynamicSquid cout is type-safe already, but like printf, you can easily do printf("%d", "die system, die"). But can you explain how the squid& works?

Profile icon
DynamicSquid

@StudentFires oh you mean like for this: squid& operator ++ () { ++variable; return *this; }?

Profile icon
xxpertHacker

@DynamicSquid yes, I have no clue as to how that's working, as I'm far from an expert on C++ classes.

Profile icon
DynamicSquid

@StudentFires oh, okay. here, think of it like this:

int nums[] = { 1, 2, 3, 4, 5 }; int& getValue(int index) { return nums[index]; } int main () { // without the reference return type, you couldn't do this getValue(2) = 7; }

So the reference return type basically turns the function into the actual value it's returning.

Profile icon
xxpertHacker

@DynamicSquid I know what references are, yet I haven't returned one. Interesting.

Profile icon
Uptight

Cool...

Profile icon
[deleted]

noice

Profile icon
DynamicSquid
Profile icon
ChezCoder

impressive!

Profile icon
DynamicSquid

@ChezCoder thanks!

Profile icon
Jakman

I dont believe this.

Profile icon
DynamicSquid

@Jakman you don't believe what?

Profile icon
Jakman

@DynamicSquid never seen the making of a data type. This is just cool to me.

Profile icon
DynamicSquid

@Jakman oh cool. it's basically just a bunch of template classes and overloaded operators

Profile icon
Jakman
Profile icon
DynamicSquid

@Jakman i think C++ is a better language for making your own data type than say python, since C++ supports some necessary features

Profile icon
Jakman

@DynamicSquid would not personally try data type making in a dynamically typed language.

Profile icon
DynamicSquid

@Jakman yeah lol