Casting is a process where an object type is explicitly converted into another type if the conversion is allowed.
2
votes
1answer
54 views
Are my implicit type conversions correct?
I'm having some issues with my code and I'm trying to rule out any syntax or programming mistakes. One of the things I'm trying to rule out is the different variable types used in my equations. I'm ...
4
votes
1answer
102 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
73 views
Could I get rid of all these downcasts?
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
60 views
Returning a more specific class
I would appreciate some feedback on my design of a few classes to query the Trakt.tv API.
public abstract class TraktQuery
{
private const string baseUrl = "http://api.trakt.tv/";
private ...
7
votes
1answer
512 views
Improve my 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 ...
3
votes
2answers
56 views
Which Casting Method Works Best
void Generic::IntToS16 (S16 & Out_, int & In_){
unsigned char * bytes_in = reinterpret_cast<unsigned char*> (&In_);
unsigned char * bytes_out = reinterpret_cast<unsigned ...
10
votes
3answers
171 views
Cast inside the method or let the client code cast, which one is better?
I have two choices of implementing a method, the first one is a generic type where the client code does not have to cast
public T GetControl<T>(string controlName) where T : Control
{
...
1
vote
2answers
170 views
(sort of) casting types in Ruby: changing class of an instance [closed]
I defined the class Rectangle:
class Rectangle
attr_reader :b, :h
def initialize(b, h)
@b = b
@h = h
end
def area
@b*@h
end
def to_s
"Rectangle #{@b}x{@h}"
end
end
and ...
2
votes
1answer
65 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:
class CRAIICall
{
public:
typedef void (WINAPI * FnType)(HANDLE);
CRAIICall(HANDLE h, FnType fun)
: m_h(h), m_fun(fun)
...
4
votes
1answer
84 views
Please review for - any unnecesary casting, memory leaks,wrong use of pthread call, or validation problem is there in the given code fragment
/**********************************************************************
* FILENAME :thread_1.c
* DESCRIPTION:Contains Code for a program that demonstrates the
* use of pthread ...
1
vote
1answer
277 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
177 views
Is there a better way to cast between decimal and generic in this C# code sample?
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
91 views
Over-Riding User Input
Here's a trivial example:
if type(length) != int:
length = 16
as opposed to:
try:
len_mod = (length + 33) / 32 + 1
except TypeError:
#recover here somehow, over-ride input?
len_mod ...
0
votes
1answer
458 views
Casting const pointer to non-const pointer when using struct iovec
struct iovec is defined in <uio.h> by the following way:
/* Structure for scatter/gather I/O. */
struct iovec
{
void *iov_base; /* Pointer to data. */
size_t iov_len; /* Length of ...
3
votes
2answers
881 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:
protected int CurrentCom
{
get
...
6
votes
2answers
178 views
C++ int_cast<> function for checked casts?
In order to detect run-time integer casting problems, I've created this function*:
template<typename TTo, typename TFrom>
static inline TTo int_cast(TFrom value)
{
TTo result = ...
2
votes
2answers
150 views
A way to do this without a lot of variables and casting?
Currently I have this code:
float tileX = (float)rectangle.X / (float)newTileSize;
float tileY = (float)rectangle.Y / (float)newTileSize;
int xOrigin = (int)Math.Round(tileX) * newTileSize;
int ...
3
votes
1answer
309 views
How to avoid unchecked cast warning in my generic recursive Iterator in Java
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 of I need recursive elements:
public interface ...
3
votes
4answers
183 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:
int CalcDistance (int y1, int x1, int *y2, int ...
8
votes
2answers
458 views
How to improve this loops?
I have the following method:
private void removeUnnecessaryLines(List<ScatterViewItem> list)
{
List<Line> remove = new List<Line>();
foreach (Line ...