type-casting

overriding bool() for custom class

All I want is for bool(myInstance) to return False (and for myInstance to evaluate to False when in a conditional like if/or/and. I know how to override >, <, =) I've tried this: class test: def __bool__(self): return False myInst = test() print bool(myInst) #prints "True" print myInst.__bool__() #prints "False" Any sugg...

Converting method signatures

Hi. typedef void (__thiscall* LPVOIDPROC) (void); class ClassA { LPVOIDPROC m_pProc; void SetProc(LPVOIDPROC pProc) { m_pProc = pProc; } void OnSomeEvent() { m_pProc(); } } class ClassB { ClassA* pCA; void Proc() { /* ... */ } void Init() { // Assume pCA != NULL pCA->Set((LPVOIDPROC)&ClassB::Proc); // error ...

VB.NET typecasting a listview tag object

In C# i would do something like: mytype val = (mytype)mylistview.SelectedItems(0).Tag; how can I do the same thing in VB.NET? ...

Different ways of type-conversion. What is the difference.

I am trying to get a difference between the type casting methods. eg. Method 1 public byte fun() { object value=1; return (byte)value; // this gives me error } Method 2 public byte fun() { object value=1; return byte.Parse(value.ToString()); // this runs } Method 3 public byte fun() { object value=1; return Co...

Need clarifications in C-style, reinterpret, and const casts

Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is t...

typecasting to unsigned in C

int a = -534; unsigned int b = (unsigned int)a; printf("%d, %d", a, b); prints -534, -534 Why is the typecast not taking place? I expected it to be -534, 534 If I modify the code to int a = -534; unsigned int b = (unsigned int)a; if(a < b) printf("%d, %d", a, b); its not printing anything... after all a is less than b?? ...

PHP Equivalent of Java Type-Casting Solution

Since PHP has no custom-class type-casting, how would I go about doing the PHP equivalent of this Java code: CustomBaseObject cusBaseObject = cusBaseObjectDao.readCustomBaseObjectById(id); ((CustomChildObject) cusBaseObject).setChildAttribute1(value1); ((CustomChildObject) cusBaseObject).setChildAttribute2(value2); In my case, it woul...

What are the Java equivalents of C# 'is' and 'as' keywords?

Question says it all. Some quick code examples of usage would be nice.. thanks! ...

What's the difference between casting using (Object as TClass) and TClass(Object)

Got an issue where MyObj.classnameis(TMyClass.classname) is true and TMyClass(MyObj) works but (MyObj as TMyclass).doSomething throws a conversion error. I don't really want any help with that junk, although if you want to put it in the comments that'd be super. I just would like to know what the difference between Obj As Class and Cl...

How to get arc4Sin() output into label/NSString

I'm trying to take the output of arc4sin and put it into a label. (EDIT: You can ignore this and just post sample code, if this is too irrelevant.) I've tried: // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; NSString *number = [[NSString...

Operator precedence and struct definition in C

struct struct0 { int a; }; struct struct1 { struct struct0 structure0; int b; } rho; &rho->structure0; /* Reference 1 */ (struct struct0 *)rho; /* Reference 2 */ (struct struct0)rho; /* Reference 3 */ From reference 1, does the compiler take the address of rho, and then access structure0, or vice-versa? What does the line at ...

Integer to Character conversion in C

Lets us consider this snippet: int s; scanf("%c",&s); Here I have used int, and not char, for variable s, now for using s for character conversion safely I have to make it char again because when scanf reads a character it only overwrites one byte of the variable it is assigning it to, and not all four that int has. For conversion I ...

Dynamically specify the type in C#

I'm creating a custom DataSet and I'm under some constrains: I want the user to specify the type of the data which they want to store. I want to reduce type-casting because I think it will be VERY expensive. I will use the data VERY frequently in my application. I don't know what type of data will be stored in the DataSet, so my init...

C# performance analysis- how to count CPU cycles?

Is this a valid way to do performance analysis? I want to get nanosecond accuracy and determine the performance of typecasting: class PerformanceTest { static double last = 0.0; static List<object> numericGenericData = new List<object>(); static List<double> numericTypedData = new List<double>(); static void Main(string...

C++: can't static_cast from double* to int*

When I try to use a static_cast to cast a double* to an int*, I get the following error: invalid static_cast from type ‘double*’ to type ‘int*’ Here is the code: #include <iostream> int main() { double* p = new double(2); int* r; r=static_cast<int*>(p); std::cout << *r << std::endl; } I understand ...

Cast element in Java For Each statement

Is it possible (or even advisable) to cast the element retrieved from a for each statement in the statement itself? I do know that each element in list will be of type <SubType>. I.E.: List<BaseType> list = DAO.getList(); for(<SubType> element : list){ // Cannot convert from element type <BaseType> to <SubType> ... } rathe...

How do i cast an object to a string when object is not a string?

-Edit- Alternative question/example http://stackoverflow.com/questions/2486873/how-do-i-cast-a-to-object-to-class-b-when-a-can-typcast-to-b I have class A, B, C. They all can implicitly convert to a string public static implicit operator A(string sz_) { ... return sz; } I have code that does this object AClassWhichImplicitlyCon...

How do i cast A to object to class A when B can typcast to A?

Basically i want to do this. aa causes a bad cast exception. NOTE: o can be ANYTHING. It may not be B, it can be C, D, E, F etc. But this should work as long as o is a class that can typecast into A (B is such a class. It uses an implicit operator overload) var b = (B)"sz"; var a = (A)b; object o = b; var...

Why is casting Derived** to Base*const* forbidden ?

After reading this question, i saw the answer by Naveen containing a link to this page, which basically says, that casting from Derived** to Base** is forbidden since could change a pointer to an pointer to a Derived1 object point to a pointer to a Derived2 object (like: *derived1PtrPtr=derived2Ptr). OK, i understand this is evil ... B...

Should I typecast in PHP when defining my vars?

I am sorry if this is more of a theory question then a real life problem but it is a real life situation for me. We were commenting on the way PHP works with vars and how memory heavy it is on the server due to its "mixed vars" and something occured to me - why not typecast right from the start? So I guess my quesion is: Whould it make ...