C ++ custom type
source link: https://www.codesd.com/item/c-custom-type.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
C ++ custom type
I've got a class int32
, and it is set up in a way that it can essentially be interfaced with as an int
(operator overloads), but there's one part I don't get.
int32 i32 = 100; // Works
int i = 200; // Works
i32 += 10; // Works
i32 -= 10; // Works, i32 = 100 right now
i = i32; // Doesn't work
What operator would I need to overload to achieve referencing i32 returning it's stored value, in this case, 100 (or how else could it be done)?
You could add a conversion operator operator int
:
class int32 {
public:
operator int() const;
...
};
Note that it comes in two flavours. The above will allow
int32 foo;
int i = foo;
If you define the conversion operator as explicit
explicit operator int() const;
then the above will fail by design, and require an explicit cast:
int32 foo;
int i = static_cast<int>(foo);
Related Articles
Haskell drifting the show for a custom type
C # - Which interfaces + operators must be implemented to get a value comparison and a tie on custom types?
The fastest way to remove std :: list duplicates from a custom type
How to define a custom type of Arrayreference of Ints in Perl 6?
How do I use qSort with custom types in QList?
How do you implement IComparable to sort an array of a custom type?
How to generate uniformly distributed random reels for custom types without reinventing the wheel?
Sort a custom type array by a string attribute?
How do I save custom types on a file on a mobile device?
MongoDB molding documents in custom types
Why are custom type definitions included in this project?
Zend expressive + doctrine custom types
Make a custom type & ldquo; tie-able & rdquo; (compatible with std :: tie)
What is the problem with my custom type for JPA / Hibernate?
Exception thrown when attempting to enumerate Custom type list
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK