|
I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts (i.e.
MyClass *m = (MyClass *)ptr;
all over the place, but there seem to be two other...
Started by Graeme Perrow on
, 11 posts
by 10 people.
Answer Snippets (Read the full thread at stackoverflow):
Dynamic_cast
Dynamic cast is used to convert pointers and references at run-time, generally
static_cast doesn't do any run time checking of the types involved, which means that unless you know Cast
dynamic....
|
|
What is implicit_cast? when should I prefer implicit_cast rather than static_cast?
Started by yesraaj on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Using no cast at all works most of the time when implicit_Cast does, that's whereimplicit_cast transforms one type to another, and can be extended by writing implicit cast functions, to cast....
Is often not needed.
|
|
Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
I don't quite get when to use static cast and when dynamic. Any explanation please?
Started by rayimag on
, 8 posts
by 8 people.
Answer Snippets (Read the full thread at stackoverflow):
It can ! :)
....
Then static_cast is not safe type of casting.
If you are talking about C++.
A compile time cast, checks if origin class can be promoted to the casted class by some simple rules at compile-time.
|
Ask your Facebook Friends
|
Try to see which cast is faster (not necessary better): new c++ case or old fashion C style cast. Any ideas?
Started by Andrei on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
No difference....
Even) optimizations.
!
They are same as it is resolved during compile time itself and there is no runtime overhead.
Shouldn't use C-cast, instead use C++ casts - it will allow you to find errors at compile time.
|
|
Hello.
When I compile the following code, I only see the error during Run-time which says "Unable to cast object of type 'Foo1' to type 'Foo2'"
Why is the compiler not showing this error during compile time?
public void Start() { Foo1 objFoo1 = new Foo...
Answer Snippets (Read the full thread at stackoverflow):
If instead you changed that to (int) objFoo1 it would bomb... .
It will then allow the code is that objFoo1 is a Foo1 , Foo2 is derived from Foo1 , and therefore the cast is legal.
Its because the compiler is obeying your instruction to cast.
|
|
Why can’t compiler detect at compile-time that obj references object of type B and thus reports an error when we try to cast it to type A ?
public class A { } public class B { } static void Main(string[] args) { B b = new B(); object obj = (object)b; ...
Started by AspOnMyNet on
, 9 posts
by 9 people.
Answer Snippets (Read the full thread at stackoverflow):
The compiler ....
Imagine if the object was passed in to the function, which then downcast it .
You want the compiler to follow the control flow, and determine ahead of time that the cast from - and then determine if the cast is valid.
|
|
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast is more efficient than using dynamic cast.
Example:
#include <iostream> #include <typeinfo> using namespace std; class Shape { public: virtual ~Shape...
Started by Ankur on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Static_cast....
Static_cast per sedynamic_cast would return NULL if you hadn't done the typeid check and the cast couldn't succeed.
Completely unrelated types, where static_cast would object with a compile-time error.
|
|
I've created two classes, with one of them having an implicit cast between them:
public class Class1 { public int Test1; } public class Class2 { public int Test2; public static implicit operator Class1(Class2 item) { return new Class1{Test1 = item.Test...
Started by Ryan Versaw on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
You have saved me a pile of time....
Because, looking at the code via Reflector, Cast doesnt attempt to take any implicit cast operators (the LINQ Cast code is heavily optimised for special cases of all kinds, but nothing somewhere.
|
|
Judging by the title of this question, what I want to do may not be possible so I will describe what I'm doing and you can feel free to let me know what I'm doing wrong and what would be a better way to accomplish my goal.
I have an XML file that describes...
Started by Sanjamal on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Casting is only useful when (cast or boxed into an Object....
You don't need the cast at all.
PropertyInfo.SetValue takes an argument of type object , so just pass o to it, and that will be it .
You don't need any cast there at all.
|
|
I have to fix a typical memory leak, Problem is like that :
typedef std::map<unsigned long,Response> mapType; class Response { public: void *dataPtr; unsigned long tag; } class anyClass { public:: DataType x; }
From client i am getting a map of ...
Started by singh on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
In this way, you don't need.
First, I don't know why do you need to cast these pointers to their original type if it is a memory the dynamic_cast(ptr) gives you some type safety at the conversion.
|