What is the difference between static_cast and Implicit_cast?
What is implicit_cast? when should I prefer implicit_cast rather than static_cast? ...
What is implicit_cast? when should I prefer implicit_cast rather than static_cast? ...
I found the following rule in a coding standards sheet : Do not rely on implicit conversion to bool in conditions. if (ptr) // wrong if (ptr != NULL) // ok How reasonable/usefull is this rule? How much overload on the compiled code? ...
Can someone tell me why the line with "//Compiles" compiles, and why the line with "//Doesn't Compile" does not? I don't understand why A would be implicitly convertible to B, not the other way round. public class SomeClass { static public void Test() { AClass a = new AClass(); BClass b = new BClass(); a = b; // Compiles b...
Is it possible to getting rid of error C2243? class B {}; class D : protected B {}; D d; B *p = &d; // conversion from 'D *' to 'B &' exists, but is inaccessible I had this error in my app and at the end I've managed to compile it by making an explicit conversion: D d; B *p = (B*)&d; I can't understand why by making class D inhe...
I have a C# library that internal clients configure with VB.Net Their scripts are throwing an InvalidCastException where they really shouldn't. So the code is something like this (massively simplified): //C#3 public class Foo { public static implicit operator Foo ( Bar input ) { return new Foo( input.Property1, input...
I'm trying to deserialize json to an object model where the collections are represented as IList<T> types. The actual deserializing is here: JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<IList<Contact>>( (new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd())); Befor...
Let's say you have yourself a class like the following: public sealed class StringToInt { private string _myString; private StringToInt(string value) { _myString = value; } public static implicit operator int(StringToInt obj) { return Convert.ToInt32(obj._myString); } public static im...
short s; s = (EitherTrueOrFalse()) ? 0 : 1; This fails with: error CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Can anyone explain why this is so? The only thing I can think of is that the compiler doesn't look at the second value and doesn't know the range...
Hello all, While looking at the System.Type class under the Code Definition Window, I cannot seem to understand how an instance of this class is implicitly cast to string. For example, on the following code: int foo = 0; Console.WriteLine("Hi! I'm a type of type {0}", foo.GetType()); How was the System.Type resulting from GetType() i...
I have a subset of a pointer class that look like: template <typename T> struct Pointer { Pointer(); Pointer(T *const x); Pointer(const Pointer &x); template <typename t> Pointer(const Pointer<t> &x); operator T *() const; }; The goal of the last constructor is to allow to pass a Pointer of a subclass, o...
I have an mvc model class created and one of the properties is of type 'MyObject'. It also has a System.ComponentModel.DataAnnotations.StringLength attribute on it. MyObject as implicit cast operators so it can essentially be used as a string: public static implicit operator string(MyObject o){...} public static implicit operator MyOb...
In my code using reflections i wrote if (f.FieldType.IsAssignableFrom("".GetType())) I have a class that has an implicit conversion to strings. However the if statement above doesnt catch it. How can i make reflection/the above if statement catch strings and classes with implicit string conversion? instead of specifically strings and ...
Given this class with an implicit cast operator: public class MyDateTime { public static implicit operator MyDateTime(System.Int64 encoded) { return new MyDateTime(encoded); } public MyDateTime(System.Int64 encoded) { _encoded = encoded; } System.Int64 _encoded; } I can now do the following...
My String class provides an operator char* overload to allow you to pass the string to C functions. Unfortunately a colleague of mine just inadvertently discovered a bug. He effectively had the following code. StringT str; // Some code. delete str; Is there anyway to prevent delete from casting the string object to a char* to prev...
My program was:- #include < iostream.h> #include < conio.h> struct base { protected: void get() { cin>>a>>b; } public: base(int i=0, int j=0); void put() { cout << a << '\t' << b << "\tput 1"; } int a,b,c; ~base() { cout << "base destroyed"; } }; class der...
I'm a little stumped by this little C# quirk: Given variables: Boolean aBoolValue; Byte aByteValue; The following compiles: if (aBoolValue) aByteValue = 1; else aByteValue = 0; But this will not: aByteValue = aBoolValue ? 1 : 0; Error says: "Cannot implicitly convert type 'int' to 'byte'." And of course, this monstr...
How does C++ determine implicit conversion/construction of objects few levels deep? for example: struct A {}; struct B: A {}; struct C { operator B() { return B(); } }; void f(A a) {} int main(void) { f(C()); } Does it create tree of all possible conversions and chooses appropriate terminal? Something else? Thanks ...
Is there any compiler that has a directive or a parameter to cast integer calculation to float implicitly. For example: float f = (1/3)*5; cout << f; the "f" is "0", because calculation's constants(1, 3, 10) are integer. I want to convert integer calculation with a compiler directive or parameter. I mean, I won't use explicit casting ...
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...
I coded some calculation stuff (I copied below a really simplifed example of what I did) like CASE2 and got bad results. Refactored the code like CASE1 and worked fine. I know there is an implicit cast in CASE 2, but not sure of the full reason. Any one could explain me what´s exactly happening below? //CASE 1, result 5.5 double a...