• Advertisement
Sign in to follow this  

typesafety questions

This topic is 2441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
[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
Sign in to follow this  

  • Advertisement