I Made my Own Data Type (C++)!
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. 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:
Here's the list of types it can hold:
- short, int, long
- float, double
And the cool thing is, each type has it's own special method! Now let's take a look at
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!
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!
What makes the int type so special is the round 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!
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).
Another special feature is the
decimal_places() method, which counts the number of decimal places. It returns and int.
And lastly, strings!
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:
Don't forget to upvote!
I don't know, I see C casts in there
(int), and I can't do
++ on the
Some of this could've had better performance, for example,
Also, why was it a function?
Really, most of this uses the C++ stdlib, makes it private, and exposes even less than
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.