Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

typesafety questions

This topic is 2565 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

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

Read up. 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.


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:

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!