Sign in to follow this  
nuclear123

typesafety questions

Recommended Posts

1.) Is typesafety apart of a languages rules that is implemented by a compiler?

2.) How is C# considered type-safe when C++ is not( isn't it partially typesafe? ), could anyone provide an example of when C++ is not typesafe?

3.) Does C++'s typesafety features only apply at compile-time and not run-time? is it possible to have typesafety at runtime in C++ or no?

4.) Does C# have typesafe at run-time? if so how?

Share this post


Link to post
Share on other sites
[quote name='nuclear123' timestamp='1305427970' post='4810934']
1.) Is typesafety apart of a languages rules that is implemented by a compiler?
[/quote]
[url="http://en.wikipedia.org/wiki/Type_safety"]Read up[/url]. It can be enforced by the compiler, by the genereated code, by something in the standard library code, or in your code.

There's few really nice examples of how C is not type safe. One of them being the standard printf formatting. Ever misplace a %s when you meant %c? or scanf myInt instead of &myInt?
The other issue is that C/C++ let you cast between data types and pointer types, including the ominous "void *" where all type information gets lost, resulting in hard to locate errors.

[quote]
3.) Does C++'s typesafety features only apply at compile-time and not run-time? is it possible to have typesafety at runtime in C++ or no?
[/quote]
Assuming you have dynamic type information turned on in the compiler options, using constructs like dynamic_cast<> will allow you to attempt to cast between possibly compatable objects.
For instance:
[code]
class A{};
class B : public A {};
class C : public A {};

A *foo = new C;
B *bar = dynamic_cast< B * >( foo ); // results in bar == NULL
C *baz = dynamic_cast< C * >( foo ); // results in baz being a valid C pointer
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this