4

C ++ custom type

 3 years ago
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.
neoserver,ios ssh client

C ++ custom type

advertisements

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

How can I tell haskell that when show is called on a list of variables of algebraic type, a "\n" should be inserted after each line? type Customer = (Int, Int, [Int]) I tried to do this: instance of Show Customer where show x = x ++ "\n&quo

C # - Which interfaces + operators must be implemented to get a value comparison and a tie on custom types?

Let's say I have a custom type like public class MyType { public string name; public int count; public MyType(string n, int c) { name = n; count = c; } } in C# and want to have "intuitive" equality comparision for instances of that object. That

The fastest way to remove std :: list duplicates from a custom type

If this has been asked before please forgive me, I could not find it. I have a custom type for which I can implement (fuzzy) equality but no < operator that is transitive. The comparison is costly but I have not many elements. I need to sort out poly

How to define a custom type of Arrayreference of Ints in Perl 6?

How to define a custom type of an Array-Reference of Ints in Perl 6? I tried this, but it doesn't work: subset Array_of_Int of Array where *.all ~~ Int; my $n = My::Class.new( option => < 22 3 4 5 > ); # Type check failed in assignment to $!optio

How do I use qSort with custom types in QList?

I have a problem with sorting a QList containing instances of a struct: class modelHeuristic { typedef struct { QString c; QString t; double value; }Saving; public: QList<Saving> list_saving; #include "klarkeright.h" klarkeRight::klarkeRig

How do you implement IComparable to sort an array of a custom type?

I wrote a program for my C# class and need to figure out how to implement IComparable to be able to sort an array of a custom type. It compiles with no errors, but throws an exception when run: System.InvalidOperationException: Failed to compare two

How to generate uniformly distributed random reels for custom types without reinventing the wheel?

I was going to use std::uniform_real_distribution with some non-builtin floating-point-like types, e.g. half_float::half or boost::multiprecision::float128. But what I get is /opt/gcc-5.2/include/c++/5.2.0/bits/random.h:1868:7: error: static assertio

Sort a custom type array by a string attribute?

I have an array of a custom type that I want to sort by one of its String attributes. For some reason, the following code is producing wrong results. Could you point out where I might have made a mistake? class PatientLNComparator implements Comparat

How do I save custom types on a file on a mobile device?

I have a list of custom types, they need to be saved/loaded to/from a file on a mobile device (Windows Mobile 6) what method would be the most suited, taking into account the limited resources of the device? EDIT: The data file will be around 2-5mbHo

MongoDB molding documents in custom types

Is there some way to read from MongoDB values to objects of my type, without converting them from SimpleDBObject? I have tried to get all documents from collection with coll.find() but when I iterate through them I get Can't find a codec for interfac

Why are custom type definitions included in this project?

I have been studying this angular 2 project and I don't understand, how src/custom-typings.d.ts is included? This file contains typescript custom type definitions. There is no import src/custom-typings.d.ts in the sources. I can rename this file as a

Zend expressive + doctrine custom types

I'm trying to map a custom type to a string. Here's my entity definition: /** * @var string * * @ORM\Column(name="type", type="string", columnDefinition="my_type_enum", nullable=false) */ But when I try to create migration (m

Make a custom type & ldquo; tie-able & rdquo; (compatible with std :: tie)

Consider I have a custom type (which I can extend): struct Foo { int a; string b; }; How can I make an instance of this object assignable to a std::tie, i.e. std::tuple of references? Foo foo = ...; int a; string b; std::tie(a, b) = foo; Failed attem

What is the problem with my custom type for JPA / Hibernate?

My custom type is (no default constructor!): package com.XXX.common; public class Email implements Serializable { private String e; public Email(String str) { e = str; } } My entity in Hibernate 3.5.6: package com.XXX.persistence; import com.XXX.comm

Exception thrown when attempting to enumerate Custom type list

I have two Lists that are being populated via JSON deserialization List<MyType> a = JsonConvert.DeserializeObject<List<MyType>>(jsonstringa); List<MyType> b = JsonConvert.DeserializeObject<List<MyType>>(jsonstringb); I

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK