Casting is a process where an object type is explicitly converted into another type if the conversion is allowed.
1
vote
1answer
57 views
Program to count number of victories in a pool of contests
I changed the program to use a dynamic array instead of a vector or storing the integers. This program works without bugs and executes relatively fast. The only doubt Istill have is how to fill the ...
4
votes
3answers
2k views
Comparing Equals() method from MSDN
I've implemented the Equals() support for my class as follows:
...
4
votes
2answers
74 views
1
vote
1answer
31 views
Implicit type class for updating maps
With the help of SO I got my code working, but I think it still could be improved, in particular I do not like using asInstanceOf. Is there some way how to avoid ...
4
votes
1answer
45 views
strncpy To strcpy Equivalence
I have this ugly function, and I feel that the entire strncpy should just be an strcpy:
...
2
votes
1answer
39 views
Treating a short like an uchar[2]
I need to create a commandword to be send to hardware and I'd like to make is as efficient as possible (without going to Assembler or similar).
The command is 16Bit long and has to be passed as an ...
2
votes
1answer
108 views
Sorting polymorphic classes
I'm learning from "Jumping into C++" by Alex Allain (sample chapter and TOC) and solved the first problem in Chapter 26 "Inheritance and Polymorphism".
I'm especially concerned about my use of ...
4
votes
1answer
82 views
A less verbose const_cast
Motivation
const_cast is a beast that I seldomly encounter. But when I do so it is mostly a bad experience.
Nobody knows what most of the C++ programmers do but ...
7
votes
1answer
165 views
Efficient generic type conversion between numeric types in F#
It's easy to write a function that adds two ints in F#:
let add x y = x + y
Actually, it's the same as:
...
5
votes
2answers
57 views
Getting a set of subscribers from a subscriberMap
In my Java event bus project, I have a private generic method that retrieves a Set<Subscriber<E>> from a private ...
1
vote
1answer
427 views
Casting between types for shared_ptr
I'm implementing an Entity-Component-System. Basically, an entity is a wrapper around an ID, a component is just a POD struct, and a System is a class that does work on components corresponding to the ...
10
votes
1answer
190 views
Here's my interface, so call me …maybe
I'm inspecting VBA code to find obsolete code constructs - namely explicit Call statements, like this:
Call DoSomething(42)
...
5
votes
5answers
1k views
Performance of object-to-string conversion
Using an OR/M I map a lot of rows from database to array of objects (~300k). In these objects some properties are marked with special attribute [Signed]. Row-by-row ...
1
vote
1answer
44 views
2
votes
2answers
177 views
8
votes
5answers
2k views
Python odd number diamond
I worked up a quick diamond program that takes only odd numbers while returning an error with even numbers or 0.
The code looks rather ugly to me, especially with the recurring ...
2
votes
1answer
50 views
Implement interface type in implementation of another interface without too much casting
I have a group of tests that must be run in similar (but different) fashion and they have setups, results, etc. which are also similar but different. Great, I thought, I'll use interfaces to define ...
1
vote
3answers
8k views
Cast an object to Decimal? or Int? etc but leave it as null if it is null
In a case like this:
object A1 = null;
Decimal? B = Convert.ToDecimal(A1);
object A2 = null;
int? C = Convert.ToInt32(A2);
Note that in case where ...
3
votes
2answers
266 views
Removing asInstance[T] from scala code
I've written this scala code and I cannot work out how/if it is possible to remove the asInstance in the definition of class ...
6
votes
1answer
840 views
Cast a raw map to a generic map using a method, cleanly and safely in a fail early manner
Casting, instanceof, and @SuppressWarnings("unchecked") are noisy. It would be nice to stuff them down into a method where they won't need to be looked at. ...
5
votes
1answer
167 views
Changing the type of an object inside a method?
First off, this is not my code. I'm curious what everyone thinks about changing the type of an object inside of a method then modifying the properties of the cast type. I wanted to use composition to ...
12
votes
3answers
1k views
Avoiding code duplication and retaining const correctness
Sometimes I run across the problem of having two member function overloads that only differ in the constness of this (and the return type, but that is less ...
5
votes
2answers
92 views
Avoiding casts in abstract types
I asked this question on Stack Overflow and in the comments someone had this to say.
In a proper design, you should almost never have to do a dynamic_cast, even if it's hidden inside some nice ...
3
votes
3answers
107 views
Limited typecasting with regex
I'm learning Python, and found a fun little video on YouTube called "Learn Python through public data hacking". In essence it uses the CTA unofficial API to do some parsing of XML data. In taking ...
4
votes
1answer
136 views
Different Constructors, Same Implementation
I have this class containing two constructors with different signatures but they do the same thing:
...
1
vote
1answer
368 views
Can casting to fragment be more efficient than using a variable?
I'm new to using fragments in Android. I'm working on an app using a sliding panel layout and I just came up with an idea for changing my fragments. I decided to create the following method:
...
6
votes
2answers
1k views
Converting a region code (short string up to 4 characters) to a 32-bit integer
There is a function that converts region code strings (1 to 4 characters and null terminator) to 32 bit integers codes to be used in maps as keys or values.
Blindly casting char* to int* is bad as it ...
6
votes
1answer
241 views
Is this a conforming implementation of duration_cast?
Sadly VS2012's duration_cast is broken, and I actually need the functionality which is broken. So, I wrote my own:
...
4
votes
1answer
994 views
Collection of Actions
I am trying to create a class to queue up a series of commands. (Not really a queue as events happen based on time). Each command has a callback (Action) that get's called. However, each Action has a ...
2
votes
2answers
99 views
NSArray and NSNumber-int conversions
I have a textView which is displaying a string. I also have an array which keeps track of where every line begins, it stores the "start index" or NSRange.location for every line.
However, when text ...
2
votes
2answers
115 views
2D model simulating water breaking a floodbank
For a homework assignment we have to write a class that simulates water breaking a floodbank (very simplistic 2D simulation).
My implementation works fine but I found that there were many downcasts ...
3
votes
1answer
450 views
Returning a more specific class
I would appreciate some feedback on my design of a few classes to query the Trakt.tv API.
...
14
votes
2answers
8k views
TryCast<T> method
This isn't urgent, it is more along the lines of trivia or a challenge. The solution works fine as it is, but I suspect it could be better.
What follows is a method I came up with a while back in a ...
10
votes
3answers
231 views
Cast inside the method or let the client code cast, which one is better? [closed]
I have two choices of implementing a method, the first one is a generic type where the client code does not have to cast
...
1
vote
2answers
3k views
(sort of) casting types in Ruby: changing class of an instance [closed]
I defined the class Rectangle:
...
3
votes
1answer
159 views
Is it safe to cast a pointer to non-void function into a pointer to void function?
I thought it was a good idea to use this in my C++ projects:
...
4
votes
1answer
118 views
Demonstration of pthread calls
Please review for any unnecessary casting, memory leaks, wrong use of pthread call, or validation problems in the given code.
...
2
votes
1answer
104 views
Nice way to using cast on TextView [closed]
Question is short. Assuming that I don't need reference itself, would this cast
((TextView) findViewById(R.id.someTextView)).setText("lala");
be better (or at ...
2
votes
1answer
867 views
Loading an Object from File with Type-Safety and Thread-Safe access
I'm attempting to write a bit of code that allows for easy and safe access to objects in a file. It seems to work great but I was curious if there was an easier way to do this or if Java already has ...
3
votes
1answer
830 views
Computing value sums in a custom grid
I have written a helper method which computes the sum of values in some custom grid, given the column indexes. The method appears to work (for a decimal - as Anthony pointed out, I need to test this ...
1
vote
2answers
97 views
Over-Riding User Input
Here's a trivial example:
if type(length) != int:
length = 16
as opposed to:
...
2
votes
1answer
5k views
Casting const pointer to non-const pointer when using struct iovec
struct iovec is defined in <uio.h> by the following way:
...
3
votes
2answers
5k views
Is this a proper way to check a viewstate of type int?
In page load I'm saving a query string int value in a viewstate. Then I save it to my DB. Here is the code I use to retrieve viewstate value and validating it:
...
14
votes
2answers
849 views
C++ int_cast<> function for checked casts?
In order to detect run-time integer casting problems, I've created this function*:
...
2
votes
2answers
166 views
6
votes
2answers
2k views
How can I avoid unchecked cast warning in my generic recursive Iterator?
It's somewhat odd that Java's collection framework has no iterator for recursive data structures. Since I needed something like this, I wrote my own. First off, I need recursive elements:
...
3
votes
4answers
369 views
Euclidian distance - optimization and casting
I'm trying to optimize a simple Euclidian distance function in C. It's for a DLL, and calculates distances from one point to many. I have:
Version 1:
...
7
votes
2answers
6k views
C++ string_cast<> template function
In C++, to simplify string conversion between std::string and std::wstring, I created the following utility template functions:
...
4
votes
2answers
855 views
Multiple explicit cast operations
This sample code works fine, but it looks awful. How would you improve this?
...
10
votes
2answers
568 views